org.glassfish.hk2.api.Factory Java Examples

The following examples show how to use org.glassfish.hk2.api.Factory. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: GuiceComponentProviderTest.java    From seed with Mozilla Public License 2.0 6 votes vote down vote up
private <T> void givenInjections(ServiceBindingBuilder<T> bindingBuilder) {
    new MockUp<GuiceComponentProvider>() {
        @Mock
        DynamicConfiguration getConfiguration(final ServiceLocator locator) {
            return dynamicConfiguration;
        }

        @Mock
        ServiceBindingBuilder<T> newFactoryBinder(final Factory<T> factory) {
            return bindingBuilder;
        }

        @Mock
        void addBinding(final BindingBuilder<T> builder, final DynamicConfiguration configuration) {
        }
    };
}
 
Example #2
Source File: JettyServerProvider.java    From browserup-proxy with Apache License 2.0 6 votes vote down vote up
private AbstractBinder proxyManagerToHkBinder(MitmProxyManager proxyManager) {
    Factory<MitmProxyManager> proxyManagerFactory = new Factory<MitmProxyManager>() {

        @Override
        public MitmProxyManager provide() {
            return proxyManager;
        }

        @Override
        public void dispose(MitmProxyManager instance) {}
    };

    return new AbstractBinder() {
        @Override
        protected void configure() {
            bindFactory(proxyManagerFactory).to(MitmProxyManager.class);
        }
    };
}
 
Example #3
Source File: PrincipalValueFactoryProvider.java    From jersey-hmac-auth with Apache License 2.0 5 votes vote down vote up
protected Factory<? extends P> createValueFactory(final Parameter parameter) {
    final HmacAuth auth = parameter.getAnnotation(HmacAuth.class);
    if (auth != null) {
        // TODO introduce type checking
        return getFactory();
    }
    return null;
}
 
Example #4
Source File: SecurityCheckFactoryProvider.java    From commafeed with Apache License 2.0 5 votes vote down vote up
@Override
protected Factory<?> createValueFactory(final Parameter parameter) {
	final Class<?> classType = parameter.getRawType();

	SecurityCheck securityCheck = parameter.getAnnotation(SecurityCheck.class);
	if (securityCheck == null)
		return null;

	if (classType.isAssignableFrom(User.class)) {
		return new SecurityCheckFactory(securityCheck.value(), securityCheck.apiKeyAllowed());
	} else {
		return null;
	}
}
 
Example #5
Source File: SessionHelperFactoryProvider.java    From commafeed with Apache License 2.0 5 votes vote down vote up
@Override
protected Factory<?> createValueFactory(final Parameter parameter) {
	final Class<?> classType = parameter.getRawType();

	Context context = parameter.getAnnotation(Context.class);
	if (context == null)
		return null;

	if (classType.isAssignableFrom(SessionHelper.class)) {
		return new SessionHelperFactory();
	} else {
		return null;
	}
}
 
Example #6
Source File: RabbitMQBundle.java    From dropwizard-experiment with MIT License 5 votes vote down vote up
public <T> Factory<MessageQueue<T>> getQueueFactory(String queueName, Class<T> type) {
    return new Factory<MessageQueue<T>>() {
        @Override
        public MessageQueue<T> provide() {
            return getQueue(queueName, type);
        }

        @Override
        public void dispose(MessageQueue<T> instance) {}
    };
}
 
Example #7
Source File: DummyAuth.java    From dropwizard-experiment with MIT License 5 votes vote down vote up
@Override
protected Factory<?> createValueFactory(Parameter parameter) {
    if (!parameter.isAnnotationPresent(Auth.class)) {
        return null;
    }

    return new AbstractContainerRequestValueFactory<Principal>() {
        public Principal provide() {
            return user;
        }
    };
}
 
Example #8
Source File: SingularityAuthFactoryProvider.java    From Singularity with Apache License 2.0 5 votes vote down vote up
@Override
protected Factory<?> createValueFactory(Parameter parameter) {
  Class<?> paramType = parameter.getRawType();
  Auth annotation = parameter.getAnnotation(Auth.class);
  if (annotation != null && paramType.isAssignableFrom(SingularityUser.class)) {
    return authFactory;
  }
  return null;
}
 
Example #9
Source File: TypeFactory.java    From shiro-jersey with Apache License 2.0 5 votes vote down vote up
@Override
public Factory<?> getValueFactory(Parameter parameter) {
    if (type.equals(parameter.getRawType()) && parameter.isAnnotationPresent(Auth.class)) {
        return this;
    }
    return null;
}
 
Example #10
Source File: HmacAuthFeature.java    From jersey-hmac-auth with Apache License 2.0 5 votes vote down vote up
protected void configure() {
    bind(PrincipalFactory.class)
            .to(PrincipalFactory.class)
            .to(new TypeLiteral<Factory<P>>() {})
            .in(Singleton.class);
    bind(PrincipalValueFactoryProvider.class)
            .to(AbstractValueFactoryProvider.class)
            .to(ValueFactoryProvider.class)
            .in(Singleton.class);
    bind(PrincipalInjectionResolver.class)
            .to(new TypeLiteral<ParamInjectionResolver<HmacAuth>>() {})
            .to(new TypeLiteral<InjectionResolver<HmacAuth>>() {})
            .in(Singleton.class);
}
 
Example #11
Source File: TypeFactory.java    From shiro-jersey with Apache License 2.0 5 votes vote down vote up
@Override
public Factory<?> getValueFactory(Parameter parameter) {
    if (type.equals(parameter.getRawType()) && parameter.isAnnotationPresent(Auth.class)) {
        return this;
    }
    return null;
}
 
Example #12
Source File: TokenFactoryProvider.java    From robe with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected Factory<?> createValueFactory(Parameter parameter) {
    Class<?> paramType = parameter.getRawType();
    RobeAuth annotation = parameter.getAnnotation(RobeAuth.class);
    if (annotation != null && paramType.isAssignableFrom(BasicToken.class)) {
        return new TokenFactory<>(annotation.required());
    }
    return null;
}
 
Example #13
Source File: SearchFactoryProvider.java    From robe with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
protected Factory<?> createValueFactory(Parameter parameter) {
    Class<?> paramType = parameter.getRawType();
    SearchParam annotation = parameter.getAnnotation(SearchParam.class);
    if (annotation != null && paramType.isAssignableFrom(SearchModel.class)) {
        return new SearchFactory();
    }
    return null;
}
 
Example #14
Source File: AuthResolver.java    From keywhiz with Apache License 2.0 5 votes vote down vote up
@Override protected Factory<?> createValueFactory(final Parameter parameter) {
  final Class<?> classType = parameter.getRawType();
  final Auth auth = parameter.getAnnotation(Auth.class);

  if (auth == null) {
    return null;
  }

  if (classType.equals(Client.class)) {
    return new AbstractContainerRequestValueFactory<Client>() {
      @Override public Client provide() {
        return clientAuthFactory.provide(getContainerRequest());
      }
    };
  }

  if (classType.equals(AutomationClient.class)) {
    return new AbstractContainerRequestValueFactory<AutomationClient>() {
      @Override public AutomationClient provide() {
        return automationClientAuthFactory.provide(getContainerRequest());
      }
    };
  }

  if (classType.equals(User.class)) {
    return new AbstractContainerRequestValueFactory<User>() {
      @Override public User provide() {
        return userAuthFactory.provide(getContainerRequest());
      }
    };
  }

  return null;
}
 
Example #15
Source File: Pac4JValueFactoryProvider.java    From jax-rs-pac4j with Apache License 2.0 5 votes vote down vote up
@Override
protected Factory<?> createValueFactory(Parameter parameter) {
    if (parameter.isAnnotationPresent(Pac4JProfileManager.class)) {
        if (ProfileManager.class.isAssignableFrom(parameter.getRawType())) {
            return manager.get();
        }

        throw new IllegalStateException("Cannot inject a Pac4J profile manager into a parameter of type "
                + parameter.getRawType().getName());
    }

    if (parameter.isAnnotationPresent(Pac4JProfile.class)) {
        if (CommonProfile.class.isAssignableFrom(parameter.getRawType())) {
            return profile.get();
        }

        if (Optional.class.isAssignableFrom(parameter.getRawType())) {
            List<ClassTypePair> ctps = ReflectionHelper.getTypeArgumentAndClass(parameter.getRawType());
            ClassTypePair ctp = (ctps.size() == 1) ? ctps.get(0) : null;
            if (ctp == null || CommonProfile.class.isAssignableFrom(ctp.rawClass())) {
                return optProfile.get();
            }
        }

        throw new IllegalStateException(
                "Cannot inject a Pac4J profile into a parameter of type " + parameter.getRawType().getName());
    }

    return null;
}
 
Example #16
Source File: RateLimit429EnforcerFilterTest.java    From ratelimitj with Apache License 2.0 5 votes vote down vote up
@Override
protected Factory<?> createValueFactory(final Parameter parameter) {
    final RateLimiting annotation = parameter.getAnnotation(RateLimiting.class);
    if (null == annotation) {
        return null;
    } else {
        return new AbstractContainerRequestValueFactory<RequestRateLimiterFactory>() {
            @Override
            public RequestRateLimiterFactory provide() {
                return requestRateLimiterFactory;
            }
        };
    }
}
 
Example #17
Source File: RateLimitBundle.java    From ratelimitj with Apache License 2.0 5 votes vote down vote up
@Override
protected Factory<RequestRateLimiterFactory> createValueFactory(final Parameter parameter) {
    final RateLimiting annotation = parameter.getAnnotation(RateLimiting.class);
    if (null == annotation) {
        return null;
    } else {
        return new AbstractContainerRequestValueFactory<RequestRateLimiterFactory>() {
            @Override
            public RequestRateLimiterFactory provide() {
                return requestRateLimiterFactory;
            }
        };
    }
}
 
Example #18
Source File: RaftApplication.java    From barge with Apache License 2.0 4 votes vote down vote up
public ResourceConfig makeResourceConfig() {
  ClusterConfig clusterConfig = HttpClusterConfig.from(new HttpReplica(uris[serverIndex]),
    remotes());

  if (!logDir.exists() && !logDir.mkdirs())
    logger.warn("failed to create directories for storing logs, bad things will happen");

  StateMachine stateMachine = new StateMachine() {
    int i = 0;

    @Override
    public Object applyOperation(@Nonnull ByteBuffer entry) {
      return i++;
    }
  };

  final JaxRsRaftModule raftModule = new JaxRsRaftModule(clusterConfig, logDir, stateMachine, 1500, transitionListeners, protocolListeners);

  injector = Optional.of(Guice.createInjector(raftModule));

  ResourceConfig resourceConfig = new ResourceConfig();

  Binder binder = new AbstractBinder() {
    @Override
    protected void configure() {
      bindFactory(new Factory<Raft>() {
            @Override
            public Raft provide() {
              return injector.get().getInstance(Raft.class);
            }

            @Override
            public void dispose(Raft raft) {
            }
          }).to(Raft.class);
      
      bindFactory(new Factory<ClusterConfig>() {
        @Override public ClusterConfig provide() {
          return injector.get().getInstance(ClusterConfig.class);
        }

        @Override public void dispose(ClusterConfig instance) {
        }
      }).to(ClusterConfig.class);
    }
  };

  resourceConfig.register(BargeResource.class);
  resourceConfig.register(Jackson.customJacksonProvider());
  resourceConfig.register(binder);

  return resourceConfig;
}
 
Example #19
Source File: GuiceComponentProvider.java    From seed with Mozilla Public License 2.0 2 votes vote down vote up
/**
 * Get a new factory instance-based service binding builder.
 *
 * @param <T>     service type.
 * @param factory service instance.
 * @return initialized binding builder.
 */
private static <T> ServiceBindingBuilder<T> newFactoryBinder(final Factory<T> factory) {
    return BindingBuilderFactory.newFactoryBinder(factory);
}