下面列出了怎么用javax.ejb.EJBContext的API类实例代码及写法,或者点击链接到github查看源代码。
@Before
public void setup() throws IllegalArgumentException, IllegalAccessException {
System.setProperty(NpeUtils.NPE_OU_PROPERTY, "iamnotaperson");
System.setProperty("dw.metadatahelper.all.auths", "A,B,C,D");
Logger.getLogger(ClassPathXmlApplicationContext.class).setLevel(Level.OFF);
Logger.getLogger(XmlBeanDefinitionReader.class).setLevel(Level.OFF);
Logger.getLogger(DefaultListableBeanFactory.class).setLevel(Level.OFF);
ClassPathXmlApplicationContext queryFactory = new ClassPathXmlApplicationContext();
queryFactory.setConfigLocation("TestQueryLogicFactory.xml");
queryFactory.refresh();
factoryConfig = queryFactory.getBean(QueryLogicFactoryConfiguration.class.getSimpleName(), QueryLogicFactoryConfiguration.class);
Whitebox.setInternalState(bean, QueryLogicFactoryConfiguration.class, factoryConfig);
Whitebox.setInternalState(bean, ClassPathXmlApplicationContext.class, queryFactory);
ctx = createMock(EJBContext.class);
logic = createMockBuilder(BaseQueryLogic.class).addMockedMethods("setLogicName", "getMaxPageSize", "getPageByteTrigger").createMock();
DatawaveUser user = new DatawaveUser(SubjectIssuerDNPair.of("CN=Poe Edgar Allan eapoe, OU=acme", "<CN=ca, OU=acme>"), UserType.USER, null, null, null,
0L);
principal = new DatawavePrincipal(Collections.singletonList(user));
}
@Before
public void setup() throws Exception {
System.setProperty(NpeUtils.NPE_OU_PROPERTY, "iamnotaperson");
System.setProperty("dw.metadatahelper.all.auths", "A,B,C,D");
connection = instance.getConnector("root", new PasswordToken(""));
if (connection.tableOperations().exists(TABLE_NAME))
connection.tableOperations().delete(TABLE_NAME);
if (connection.tableOperations().exists(INDEX_TABLE_NAME))
connection.tableOperations().delete(INDEX_TABLE_NAME);
DatawaveUser user = new DatawaveUser(SubjectIssuerDNPair.of(userDN, "CN=ca, OU=acme"), UserType.USER, Arrays.asList(auths), null, null, 0L);
principal = new DatawavePrincipal(Collections.singletonList(user));
connectionFactory = createMock(AccumuloConnectionFactory.class);
ctx = createStrictMock(EJBContext.class);
bean = new MapReduceStatePersisterBean();
field(MapReduceStatePersisterBean.class, "connectionFactory").set(bean, connectionFactory);
field(MapReduceStatePersisterBean.class, "ctx").set(bean, ctx);
Logger.getLogger(MapReduceStatePersisterBean.class).setLevel(Level.OFF);
}
public void lookupSessionContext() throws TestFailureException {
try {
try {
final InitialContext ctx = new InitialContext();
Assert.assertNotNull("The InitialContext is null", ctx);
// lookup in enc
final SessionContext sctx = (SessionContext) ctx.lookup("java:comp/env/sessioncontext");
Assert.assertNotNull("The SessionContext got from java:comp/env/sessioncontext is null", sctx);
// lookup using global name
final EJBContext ejbCtx = (EJBContext) ctx.lookup("java:comp/EJBContext");
Assert.assertNotNull("The SessionContext got from java:comp/EJBContext is null ", ejbCtx);
// verify context was set via legacy set method
Assert.assertNotNull("The SessionContext is null from setter method", ejbContext);
} catch (final Exception e) {
Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
} catch (final AssertionFailedError afe) {
throw new TestFailureException(afe);
}
}
public void lookupSessionContext() throws TestFailureException {
try {
try {
final InitialContext ctx = new InitialContext();
Assert.assertNotNull("The InitialContext is null", ctx);
// lookup in enc
final SessionContext sctx = (SessionContext) ctx.lookup("java:comp/env/sessioncontext");
Assert.assertNotNull("The SessionContext got from java:comp/env/sessioncontext is null", sctx);
// lookup using global name
final EJBContext ejbCtx = (EJBContext) ctx.lookup("java:comp/EJBContext");
Assert.assertNotNull("The SessionContext got from java:comp/EJBContext is null ", ejbCtx);
// verify context was set via legacy set method
Assert.assertNotNull("The SessionContext is null from setter method", ejbContext);
} catch (final Exception e) {
Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
} catch (final AssertionFailedError afe) {
throw new TestFailureException(afe);
}
}
public void lookupSessionContext() throws TestFailureException {
try {
try {
final InitialContext ctx = new InitialContext();
Assert.assertNotNull("The InitialContext is null", ctx);
// lookup in enc
final SessionContext sctx = (SessionContext) ctx.lookup("java:comp/env/sessioncontext");
Assert.assertNotNull("The SessionContext got from java:comp/env/sessioncontext is null", sctx);
// lookup using global name
final EJBContext ejbCtx = (EJBContext) ctx.lookup("java:comp/EJBContext");
Assert.assertNotNull("The SessionContext got from java:comp/EJBContext is null ", ejbCtx);
// verify context was set via legacy set method
Assert.assertNotNull("The SessionContext is null from setter method", ejbContext);
} catch (final Exception e) {
Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
} catch (final AssertionFailedError afe) {
throw new TestFailureException(afe);
}
}
public void lookupSessionContext() throws TestFailureException {
try {
try {
final InitialContext ctx = new InitialContext();
Assert.assertNotNull("The InitialContext is null", ctx);
// lookup in enc
final SessionContext sctx = (SessionContext) ctx.lookup("java:comp/env/sessioncontext");
Assert.assertNotNull("The SessionContext got from java:comp/env/sessioncontext is null", sctx);
// lookup using global name
final EJBContext ejbCtx = (EJBContext) ctx.lookup("java:comp/EJBContext");
Assert.assertNotNull("The SessionContext got from java:comp/EJBContext is null ", ejbCtx);
// verify context was set via legacy set method
Assert.assertNotNull("The SessionContext is null from setter method", ejbContext);
} catch (final Exception e) {
Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
} catch (final AssertionFailedError afe) {
throw new TestFailureException(afe);
}
}
public void lookupSessionContext() throws TestFailureException {
try {
try {
final InitialContext ctx = new InitialContext();
Assert.assertNotNull("The InitialContext is null", ctx);
// lookup in enc
final SessionContext sctx = (SessionContext) ctx.lookup("java:comp/env/sessioncontext");
Assert.assertNotNull("The SessionContext got from java:comp/env/sessioncontext is null", sctx);
// lookup using global name
final EJBContext ejbCtx = (EJBContext) ctx.lookup("java:comp/EJBContext");
Assert.assertNotNull("The SessionContext got from java:comp/EJBContext is null ", ejbCtx);
} catch (final Exception e) {
Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
} catch (final AssertionFailedError afe) {
throw new TestFailureException(afe);
}
}
public void lookupSessionContext() throws TestFailureException {
try {
try {
final InitialContext ctx = new InitialContext();
Assert.assertNotNull("The InitialContext is null", ctx);
// lookup in enc
final SessionContext sctx = (SessionContext) ctx.lookup("java:comp/env/sessioncontext");
Assert.assertNotNull("The SessionContext got from java:comp/env/sessioncontext is null", sctx);
// lookup using global name
final EJBContext ejbCtx = (EJBContext) ctx.lookup("java:comp/EJBContext");
Assert.assertNotNull("The SessionContext got from java:comp/EJBContext is null ", ejbCtx);
// verify context was set via legacy set method
Assert.assertNotNull("The SessionContext is null from setter method", ejbContext);
} catch (final Exception e) {
Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
} catch (final AssertionFailedError afe) {
throw new TestFailureException(afe);
}
}
public void lookupSessionContext() throws TestFailureException {
try {
try {
final InitialContext ctx = new InitialContext();
Assert.assertNotNull("The InitialContext is null", ctx);
// lookup in enc
final SessionContext sctx = (SessionContext) ctx.lookup("java:comp/env/sessioncontext");
Assert.assertNotNull("The SessionContext got from java:comp/env/sessioncontext is null", sctx);
// lookup using global name
final EJBContext ejbCtx = (EJBContext) ctx.lookup("java:comp/EJBContext");
Assert.assertNotNull("The SessionContext got from java:comp/EJBContext is null ", ejbCtx);
// verify context was set via legacy set method
Assert.assertNotNull("The SessionContext is null from setter method", ejbContext);
} catch (final Exception e) {
Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
} catch (final AssertionFailedError afe) {
throw new TestFailureException(afe);
}
}
public void lookupSessionContext() throws TestFailureException {
try {
try {
final InitialContext ctx = new InitialContext();
Assert.assertNotNull("The InitialContext is null", ctx);
// lookup in enc
final SessionContext sctx = (SessionContext) ctx.lookup("java:comp/env/sessioncontext");
Assert.assertNotNull("The SessionContext got from java:comp/env/sessioncontext is null", sctx);
// lookup using global name
final EJBContext ejbCtx = (EJBContext) ctx.lookup("java:comp/EJBContext");
Assert.assertNotNull("The SessionContext got from java:comp/EJBContext is null ", ejbCtx);
} catch (final Exception e) {
Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
} catch (final AssertionFailedError afe) {
throw new TestFailureException(afe);
}
}
public void lookupSessionContext() throws TestFailureException {
try {
try {
final InitialContext ctx = new InitialContext();
Assert.assertNotNull("The InitialContext is null", ctx);
// lookup in enc
final SessionContext sctx = (SessionContext) ctx.lookup("java:comp/env/sessioncontext");
Assert.assertNotNull("The SessionContext got from java:comp/env/sessioncontext is null", sctx);
// lookup using global name
final EJBContext ejbCtx = (EJBContext) ctx.lookup("java:comp/EJBContext");
Assert.assertNotNull("The SessionContext got from java:comp/EJBContext is null ", ejbCtx);
// verify context was set via legacy set method
Assert.assertNotNull("The SessionContext is null from setter method", ejbContext);
} catch (final Exception e) {
Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
} catch (final AssertionFailedError afe) {
throw new TestFailureException(afe);
}
}
@Override
public void lookupMessageDrivenContext() throws TestFailureException {
try {
try {
final InitialContext ctx = new InitialContext();
Assert.assertNotNull("The InitialContext is null", ctx);
// lookup in enc
final MessageDrivenContext sctx = (MessageDrivenContext) ctx.lookup("java:comp/env/mdbcontext");
Assert.assertNotNull("The MessageDrivenContext got from java:comp/env/mdbcontext is null", sctx);
// lookup using global name
final EJBContext ejbCtx = (EJBContext) ctx.lookup("java:comp/EJBContext");
Assert.assertNotNull("The MessageDrivenContext got from java:comp/EJBContext is null ", ejbCtx);
// verify context was set via legacy set method
Assert.assertNotNull("The MessageDrivenContext is null from setter method", mdbContext);
} catch (final Exception e) {
Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
} catch (final AssertionFailedError afe) {
throw new TestFailureException(afe);
}
}
public void lookupMessageDrivenContext() throws TestFailureException {
try {
try {
final InitialContext ctx = new InitialContext();
Assert.assertNotNull("The InitialContext is null", ctx);
// lookup in enc
final MessageDrivenContext sctx = (MessageDrivenContext) ctx.lookup("java:comp/env/mdbcontext");
Assert.assertNotNull("The MessageDrivenContext got from java:comp/env/mdbcontext is null", sctx);
// lookup using global name
final EJBContext ejbCtx = (EJBContext) ctx.lookup("java:comp/EJBContext");
Assert.assertNotNull("The MessageDrivenContext got from java:comp/EJBContext is null ", ejbCtx);
} catch (final Exception e) {
Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
} catch (final AssertionFailedError afe) {
throw new TestFailureException(afe);
}
}
@Override
public void lookupMessageDrivenContext() throws TestFailureException {
try {
try {
final InitialContext ctx = new InitialContext();
Assert.assertNotNull("The InitialContext is null", ctx);
// lookup in enc
final MessageDrivenContext messageDrivenContext = (MessageDrivenContext) ctx.lookup("java:comp/env/mdbcontext");
Assert.assertNotNull("The SessionContext got from java:comp/env/mdbcontext is null", messageDrivenContext);
// lookup using global name
final EJBContext ejbCtx = (EJBContext) ctx.lookup("java:comp/EJBContext");
Assert.assertNotNull("The SessionContext got from java:comp/EJBContext is null ", ejbCtx);
// verify context was set via legacy set method
Assert.assertNotNull("The MdbContext is null from setter method", mdbContext);
} catch (final Exception e) {
Assert.fail("Received Exception " + e.getClass() + " : " + e.getMessage());
}
} catch (final AssertionFailedError afe) {
throw new TestFailureException(afe);
}
}
@Override
public void deploy(final BeanContext beanContext) throws OpenEJBException {
synchronized (this) {
final Object deploymentId = beanContext.getDeploymentID();
cmpEngine.deploy(beanContext);
beanContext.setContainerData(cmpEngine);
beanContext.set(EJBContext.class, new EntityContext(securityService));
// try to set deploymentInfo static field on bean implementation class
try {
final Field field = beanContext.getCmpImplClass().getField("deploymentInfo");
field.set(null, beanContext);
} catch (final Exception e) {
// ignore
}
// add to indexes
deploymentsById.put(deploymentId, beanContext);
beansByClass.put(beanContext.getCmpImplClass(), beanContext);
beanContext.setContainer(this);
}
}
/**
* Creates a MdbInstanceFactory for a single specific deployment.
*
* @param beanContext the deployment for which instances will be created
* @param securityService the transaction manager for this container system
* @param instanceLimit the maximal number of instances or <= 0 if unlimited
*/
public MdbInstanceFactory(final BeanContext beanContext, final SecurityService securityService, final int instanceLimit) throws OpenEJBException {
this.beanContext = beanContext;
this.instanceLimit = instanceLimit;
mdbContext = new MdbContext(securityService);
try {
final Context context = beanContext.getJndiEnc();
context.bind("comp/EJBContext", mdbContext);
context.bind("comp/TimerService", new TimerServiceWrapper());
} catch (final NamingException e) {
throw new OpenEJBException("Failed to bind EJBContext/TimerService", e);
}
beanContext.set(EJBContext.class, this.mdbContext);
}
private <R extends JndiReference> boolean isPrivateReference(final R a) {
if (!isResourceRef(a)) {
return false;
}
final Class[] types = {EJBContext.class, EntityContext.class, SessionContext.class, MessageDrivenContext.class, UserTransaction.class};
for (final Class type : types) {
if (type.getName().equals(a.getType())) {
return true;
}
}
return false;
}
/**
* Constructor
*
* @param configuration
* Configuration bean for lookupUUID web service endpoints
* @param queryExecutor
* Service that executes queriesoptionalParamsToMap
* @param context
* The EJB's content
*/
public LookupUUIDUtil(final LookupUUIDConfiguration configuration, final QueryExecutor queryExecutor, final EJBContext context,
final ResponseObjectFactory responseObjectFactory) {
// Validate and assign the lookup UUID configuration
if (null == configuration) {
throw new IllegalArgumentException("Non-null configuration required to lookup UUIDs");
}
this.lookupUUIDConfiguration = configuration;
// Validate and assign the query executor
if (null == queryExecutor) {
throw new IllegalArgumentException("Non-null query executor required to lookup UUIDs");
}
this.queryExecutor = queryExecutor;
// Assign the EJB context
this.ctx = context;
// Assign the field event factory needed for the response objects
this.responseObjectFactory = responseObjectFactory;
// Populate the UUIDType map
final List<UUIDType> types = this.lookupUUIDConfiguration.getUuidTypes();
this.uuidTypes.clear();
if (null != types) {
for (final UUIDType type : types) {
if (null != type) {
this.uuidTypes.put(type.getFieldName().toUpperCase(), type);
}
}
}
// Assign the begin date
try {
this.beginAsDate = DateHelper.parseWithGMT(this.lookupUUIDConfiguration.getBeginDate());
} catch (DateTimeParseException e) {
this.log.error(e.getMessage(), e);
}
// Assign the maximum number of UUIDs allowed for batch lookup. A zero or negative
// value is interpreted as unlimited, which is automatically adjusted to -1.
this.maxAllowedBatchLookupUUIDs = this.lookupUUIDConfiguration.getBatchLookupUpperLimit();
if (this.maxAllowedBatchLookupUUIDs <= 0) {
this.maxAllowedBatchLookupUUIDs = -1;
}
this.defaultOptionalParams = this.lookupUUIDConfiguration.optionalParamsToMap();
}
@Before
public void setup() throws Exception {
System.setProperty(NpeUtils.NPE_OU_PROPERTY, "iamnotaperson");
System.setProperty("dw.metadatahelper.all.auths", "A,B,C,D");
QueryTraceCache traceCache = new QueryTraceCache();
Whitebox.invokeMethod(traceCache, "init");
cache = new QueryCache();
cache.init();
closedCache = new ClosedQueryCache();
bean = new QueryExecutorBean();
connectionFactory = createStrictMock(AccumuloConnectionFactory.class);
auditor = new AuditBean();
auditService = createStrictMock(AuditService.class);
metrics = createStrictMock(QueryMetricsBean.class);
queryLogicFactory = createStrictMock(QueryLogicFactoryImpl.class);
persister = createStrictMock(Persister.class);
predictor = createStrictMock(QueryPredictor.class);
ctx = createStrictMock(EJBContext.class);
qlCache = new CreatedQueryLogicCacheBean();
queryExpirationConf = new QueryExpirationConfiguration();
queryExpirationConf.setPageSizeShortCircuitCheckTime(45);
queryExpirationConf.setPageShortCircuitTimeout(58);
queryExpirationConf.setCallTime(60);
connectionRequestBean = createStrictMock(AccumuloConnectionRequestBean.class);
setInternalState(auditor, AuditService.class, auditService);
setInternalState(auditor, AuditParameterBuilder.class, new DefaultAuditParameterBuilder());
setInternalState(connectionRequestBean, EJBContext.class, ctx);
setInternalState(bean, QueryCache.class, cache);
setInternalState(bean, ClosedQueryCache.class, closedCache);
setInternalState(bean, AccumuloConnectionFactory.class, connectionFactory);
setInternalState(bean, AuditBean.class, auditor);
setInternalState(bean, QueryMetricsBean.class, metrics);
setInternalState(bean, QueryLogicFactory.class, queryLogicFactory);
setInternalState(bean, QueryExpirationConfiguration.class, queryExpirationConf);
setInternalState(bean, Persister.class, persister);
setInternalState(bean, QueryPredictor.class, predictor);
setInternalState(bean, EJBContext.class, ctx);
setInternalState(bean, CreatedQueryLogicCacheBean.class, qlCache);
setInternalState(bean, QueryTraceCache.class, traceCache);
setInternalState(bean, Multimap.class, HashMultimap.create());
setInternalState(bean, LookupUUIDConfiguration.class, new LookupUUIDConfiguration());
setInternalState(bean, SecurityMarking.class, new ColumnVisibilitySecurityMarking());
setInternalState(bean, QueryParameters.class, new QueryParametersImpl());
setInternalState(bean, QueryMetricFactory.class, new QueryMetricFactoryImpl());
setInternalState(bean, AccumuloConnectionRequestBean.class, connectionRequestBean);
// RESTEasy mock stuff
dispatcher = MockDispatcherFactory.createDispatcher();
dispatcher.getRegistry().addSingletonResource(bean, "/DataWave/Query");
response = new MockHttpResponse();
}
public EjbJpaTxnManager(EJBContext context) {
super();
this.context = context;
}
public EjbJpaTxnManager(EJBContext context) {
super();
this.context = context;
}
protected EJBContext getEJBContext() {
return this.ejbContext;
}
@Override
protected EJBContext getEJBContext() {
return this.ejbContext;
}
protected EJBContext getEJBContext() {
return this.sessionContext;
}
public void deploy(final BeanContext beanContext) {
poolMap.put(beanContext.getDeploymentID(), new LinkedListStack(poolsize / 2));
beanContext.set(EJBContext.class, createEntityContext());
}
/**
* Insure that timer methods can be invoked for the current operation on this Context.
*/
private void checkState() throws IllegalStateException {
final BaseContext context = (BaseContext) deployment.get(EJBContext.class);
context.doCheck(BaseContext.Call.timerMethod);
}
public EJBContext getInjectedContext() {
return injectedContext;
}
/**
* Returns the EJB context that was active when this class was created.
*
* @return
*/
public EJBContext getContext() {
return ctx;
}
abstract protected EJBContext getEJBContext();
EJBContext getInjectedContext();