Java Code Examples for org.apache.tomcat.util.descriptor.web.ContextEnvironment#setValue()

The following examples show how to use org.apache.tomcat.util.descriptor.web.ContextEnvironment#setValue() . 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: TestTomcat.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testEnableNaming() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Enable JNDI - it is disabled by default
    tomcat.enableNaming();

    ContextEnvironment environment = new ContextEnvironment();
    environment.setType("java.lang.String");
    environment.setName(HelloWorldJndi.JNDI_ENV_NAME);
    environment.setValue("Tomcat User");
    ctx.getNamingResources().addEnvironment(environment);

    Tomcat.addServlet(ctx, "jndiServlet", new HelloWorldJndi());
    ctx.addServletMappingDecoded("/", "jndiServlet");

    tomcat.start();

    ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
    Assert.assertEquals("Hello, Tomcat User", res.toString());
}
 
Example 2
Source File: TestNamingContextListener.java    From Tomcat8-Source-Read with MIT License 6 votes vote down vote up
@Test
public void testBug49132() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Enable JNDI - it is disabled by default
    tomcat.enableNaming();

    ContextEnvironment environment = new ContextEnvironment();
    environment.setType(BUG49132_VALUE.getClass().getName());
    environment.setName(BUG49132_NAME);
    environment.setValue(BUG49132_VALUE);
    ctx.getNamingResources().addEnvironment(environment);

    ctx.addApplicationListener(Bug49132Listener.class.getName());

    tomcat.start();

    Assert.assertEquals(LifecycleState.STARTED, ctx.getState());
}
 
Example 3
Source File: TestTomcat.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testEnableNamingGlobal() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Enable JNDI - it is disabled by default
    tomcat.enableNaming();

    ContextEnvironment environment = new ContextEnvironment();
    environment.setType("java.lang.String");
    environment.setName("globalTest");
    environment.setValue("Tomcat User");
    tomcat.getServer().getGlobalNamingResources().addEnvironment(environment);

    ContextResourceLink link = new ContextResourceLink();
    link.setGlobal("globalTest");
    link.setName(HelloWorldJndi.JNDI_ENV_NAME);
    link.setType("java.lang.String");
    ctx.getNamingResources().addResourceLink(link);

    Tomcat.addServlet(ctx, "jndiServlet", new HelloWorldJndi());
    ctx.addServletMappingDecoded("/", "jndiServlet");

    tomcat.start();

    ByteChunk res = getUrl("http://localhost:" + getPort() + "/");
    Assert.assertEquals("Hello, Tomcat User", res.toString());
}
 
Example 4
Source File: TestNamingContextListener.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testBug54096() throws Exception {
    Tomcat tomcat = getTomcatInstance();

    // No file system docBase required
    Context ctx = tomcat.addContext("", null);

    // Enable JNDI - it is disabled by default
    tomcat.enableNaming();

    ContextEnvironment environmentA = new ContextEnvironment();
    environmentA.setType(Bug54096EnvA.class.getName());
    environmentA.setName(BUG54096_NameA);
    environmentA.setValue(BUG54096_ValueA);
    ctx.getNamingResources().addEnvironment(environmentA);

    ContextEnvironment environmentB = new ContextEnvironment();
    environmentB.setType(Bug54096EnvB.class.getName());
    environmentB.setName(BUG54096_NameB);
    environmentB.setValue(BUG54096_ValueB);
    ctx.getNamingResources().addEnvironment(environmentB);

    ctx.addApplicationListener(Bug54096Listener.class.getName());

    tomcat.start();

    Assert.assertEquals(LifecycleState.STARTED, ctx.getState());
}
 
Example 5
Source File: TestNamingContext.java    From Tomcat8-Source-Read with MIT License 5 votes vote down vote up
@Test
public void testBug52830() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();

    // No file system docBase required
    org.apache.catalina.Context ctx = tomcat.addContext("", null);

    // Create the resource
    ContextEnvironment env = new ContextEnvironment();
    env.setName("boolean");
    env.setType(Boolean.class.getName());
    env.setValue("true");
    ctx.getNamingResources().addEnvironment(env);

    // Map the test Servlet
    Bug52830Servlet bug52830Servlet = new Bug52830Servlet();
    Tomcat.addServlet(ctx, "bug52830Servlet", bug52830Servlet);
    ctx.addServletMappingDecoded("/", "bug52830Servlet");

    tomcat.start();

    ByteChunk bc = new ByteChunk();
    int rc = getUrl("http://localhost:" + getPort() + "/", bc, null);
    Assert.assertEquals(200, rc);
    Assert.assertTrue(bc.toString().contains("truetrue"));
}
 
Example 6
Source File: HeartbeatTest.java    From TeaStore with Apache License 2.0 5 votes vote down vote up
private Tomcat createClientTomcat(Service service, Tomcat tomcat) throws ServletException, LifecycleException {
		int clientPort = getNextClientPort();
		tomcat.getEngine().setName("Catalina" + clientPort);
		tomcat.setPort(clientPort);
		tomcat.setBaseDir(testWorkingDir);
		tomcat.enableNaming();
		Context context = tomcat.addWebapp("/" + service.getServiceName(), testWorkingDir);
		ContextEnvironment registryURL = new ContextEnvironment();
		registryURL.setDescription("");
		registryURL.setOverride(false);
		registryURL.setType("java.lang.String");
		registryURL.setName("registryURL");
		registryURL.setValue("http://localhost:" + getRegistryPort() + "/test/rest/services/");
		context.getNamingResources().addEnvironment(registryURL);
		ContextEnvironment servicePort = new ContextEnvironment();
		servicePort.setDescription("");
		servicePort.setOverride(false);
		servicePort.setType("java.lang.String");
	    servicePort.setName("servicePort");
	    servicePort.setValue("" + clientPort);
		context.getNamingResources().addEnvironment(servicePort);		
		context.addApplicationListener(TestRegistryClientStartup.class.getName());
//		HeartbeatServlet heartbeatServlet = new HeartbeatServlet();
//		tomcat.addServlet("/" + service.getServiceName(), "heartbeatServlet", heartbeatServlet);
//		context.addServletMappingDecoded("/heartbeat", "heartbeatServlet");
		tomcat.start();
		return tomcat;
	}
 
Example 7
Source File: TestNamingContext.java    From Tomcat8-Source-Read with MIT License 4 votes vote down vote up
@Test
public void testGlobalNaming() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    tomcat.enableNaming();

    org.apache.catalina.Context ctx = tomcat.addContext("", null);

    tomcat.start();

    Context webappInitial = ContextBindings.getContext(ctx);

    // Nothing added at the moment so should be null
    Object obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
    Assert.assertNull(obj);

    ContextEnvironment ce = new ContextEnvironment();
    ce.setName(GLOBAL_NAME);
    ce.setValue(DATA);
    ce.setType(DATA.getClass().getName());

    tomcat.getServer().getGlobalNamingResources().addEnvironment(ce);

    // No link so still should be null
    obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
    Assert.assertNull(obj);

    // Now add a resource link to the context
    ContextResourceLink crl = new ContextResourceLink();
    crl.setGlobal(GLOBAL_NAME);
    crl.setName(LOCAL_NAME);
    crl.setType(DATA.getClass().getName());
    ctx.getNamingResources().addResourceLink(crl);

    // Link exists so should be OK now
    obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
    Assert.assertEquals(DATA, obj);

    // Try shortcut
    ResourceLinkFactory factory = new ResourceLinkFactory();
    ResourceLinkRef rlr = new ResourceLinkRef(DATA.getClass().getName(), GLOBAL_NAME, null, null);
    obj = factory.getObjectInstance(rlr, null, null, null);
    Assert.assertEquals(DATA, obj);

    // Remove the link
    ctx.getNamingResources().removeResourceLink(LOCAL_NAME);

    // No link so should be null
    obj = doLookup(webappInitial, COMP_ENV + "/" + LOCAL_NAME);
    Assert.assertNull(obj);

    // Shortcut should fail too
    obj = factory.getObjectInstance(rlr, null, null, null);
    Assert.assertNull(obj);
}
 
Example 8
Source File: AbstractUiTest.java    From TeaStore with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() throws ServletException, LifecycleException, InterruptedException, JsonProcessingException {
	webUITomcat = new Tomcat();
	webUITomcat.setPort(3000);
	webUITomcat.setBaseDir(testWorkingDir);
	webUITomcat.enableNaming();
	Context context = webUITomcat.addWebapp(CONTEXT, System.getProperty("user.dir") + File.separator + "src"
			+ File.separator + "main" + File.separator + "webapp");
	ContextEnvironment registryURL = new ContextEnvironment();
	registryURL.setDescription("");
	registryURL.setOverride(false);
	registryURL.setType("java.lang.String");
	registryURL.setName("registryURL");
	registryURL.setValue("http://localhost:9001/tools.descartes.teastore.registry/rest/services/");
	context.getNamingResources().addEnvironment(registryURL);
	webUITomcat.addServlet(CONTEXT, "servlet", getServlet());
	webUITomcat.addServlet(CONTEXT, "index", new IndexServlet());
	webUITomcat.addServlet(CONTEXT, "login", new LoginServlet());
	webUITomcat.addServlet(CONTEXT, "order", new OrderServlet());
	context.addServletMappingDecoded("/test", "servlet");
	context.addServletMappingDecoded("/index", "index");
	context.addServletMappingDecoded("/login", "login");
	context.addServletMappingDecoded("/order", "order");
	context.addWelcomeFile("/index");
	webUITomcat.start();

	// Mock registry
	List<String> strings = new LinkedList<String>();
	strings.add("localhost:9001");
	String json = new ObjectMapper().writeValueAsString(strings);
	wireMockRule.stubFor(get(urlEqualTo(
			"/tools.descartes.teastore.registry/rest/services/" + Service.IMAGE.getServiceName() + "/"))
					.willReturn(okJson(json)));
	wireMockRule.stubFor(get(urlEqualTo(
			"/tools.descartes.teastore.registry/rest/services/" + Service.AUTH.getServiceName() + "/"))
					.willReturn(okJson(json)));
	wireMockRule.stubFor(get(urlEqualTo(
			"/tools.descartes.teastore.registry/rest/services/" + Service.PERSISTENCE.getServiceName() + "/"))
					.willReturn(okJson(json)));
	wireMockRule.stubFor(get(urlEqualTo(
			"/tools.descartes.teastore.registry/rest/services/" + Service.RECOMMENDER.getServiceName() + "/"))
					.willReturn(okJson(json)));

	// Mock images
	HashMap<String, String> img = new HashMap<>();
	img.put("andreBauer", "andreBauer");
	img.put("johannesGrohmann", "johannesGrohmann");
	img.put("joakimKistowski", "joakimKistowski");
	img.put("simonEismann", "simonEismann");
	img.put("norbertSchmitt", "norbertSchmitt");
	img.put("descartesLogo", "descartesLogo");
	img.put("icon", "icon");
	mockValidPostRestCall(img, "/tools.descartes.teastore.image/rest/image/getWebImages");
}
 
Example 9
Source File: AbstractRecommenderRestTest.java    From TeaStore with Apache License 2.0 4 votes vote down vote up
/**
 * Sets up a registry, a persistance unit and a store.
 * 
 * @throws Throwable
 *             Throws uncaught throwables for test to fail.
 */
@Before
public void setup() throws Throwable {
	persistence = new MockPersistenceProvider(persistenceWireMockRule);

	otherrecommender = new MockOtherRecommenderProvider(otherRecommenderWireMockRule);

	setRegistry(new MockRegistry(registryWireMockRule, Arrays.asList(getPersistence().getPort()),
			Arrays.asList(RECOMMENDER_TEST_PORT, otherrecommender.getPort())));

	// debuggging response
	// Response response1 = ClientBuilder
	// .newBuilder().build().target("http://localhost:" + otherrecommender.getPort()
	// + "/"
	// + Service.RECOMMENDER.getServiceName() + "/rest/train/timestamp")
	// .request(MediaType.APPLICATION_JSON).get();
	// System.out.println(response1.getStatus() + ":" +
	// response1.readEntity(String.class));

	// debuggging response
	// Response response0 = ClientBuilder.newBuilder().build()
	// .target("http://localhost:" + MockRegistry.DEFAULT_MOCK_REGISTRY_PORT
	// + "/tools.descartes.teastore.registry/rest/services/"
	// + Service.PERSISTENCE.getServiceName() + "/")
	// .request(MediaType.APPLICATION_JSON).get();
	// System.out.println(response0.getStatus() + ":" +
	// response0.readEntity(String.class));
	//
	// Response response1 = ClientBuilder.newBuilder().build()
	// .target("http://localhost:" + persistence.getPort()
	// + "/tools.descartes.teastore.persistence/rest/orderitems")
	// .request(MediaType.APPLICATION_JSON).get();
	// System.out.println(response1.getStatus() + ":" +
	// response1.readEntity(String.class));

	// Setup recommend tomcat
	testTomcat = new Tomcat();
	testTomcat.setPort(RECOMMENDER_TEST_PORT);
	testTomcat.setBaseDir(testWorkingDir);
	testTomcat.enableNaming();
	Context context = testTomcat.addWebapp("/" + Service.RECOMMENDER.getServiceName(), testWorkingDir);
	ContextEnvironment registryURL3 = new ContextEnvironment();
	registryURL3.setDescription("");
	registryURL3.setOverride(false);
	registryURL3.setType("java.lang.String");
	registryURL3.setName("registryURL");
	registryURL3.setValue(
			"http://localhost:" + registry.getPort() + "/tools.descartes.teastore.registry/rest/services/");
	context.getNamingResources().addEnvironment(registryURL3);
	ContextEnvironment servicePort3 = new ContextEnvironment();
	servicePort3.setDescription("");
	servicePort3.setOverride(false);
	servicePort3.setType("java.lang.String");
	servicePort3.setName("servicePort");
	servicePort3.setValue("" + RECOMMENDER_TEST_PORT);
	context.getNamingResources().addEnvironment(servicePort3);
	ResourceConfig restServletConfig3 = new ResourceConfig();
	restServletConfig3.register(TrainEndpoint.class);
	restServletConfig3.register(RecommendEndpoint.class);
	restServletConfig3.register(RecommendSingleEndpoint.class);
	ServletContainer restServlet3 = new ServletContainer(restServletConfig3);
	testTomcat.addServlet("/" + Service.RECOMMENDER.getServiceName(), "restServlet", restServlet3);
	context.addServletMappingDecoded("/rest/*", "restServlet");
	context.addApplicationListener(RecommenderStartup.class.getName());

	ContextEnvironment recommender = new ContextEnvironment();
	recommender.setDescription("");
	recommender.setOverride(false);
	recommender.setType("java.lang.String");
	recommender.setName("recommenderAlgorithm");
	recommender.setValue("PreprocessedSlopeOne");
	context.getNamingResources().addEnvironment(recommender);

	ContextEnvironment retrainlooptime = new ContextEnvironment();
	retrainlooptime.setDescription("");
	retrainlooptime.setOverride(false);
	retrainlooptime.setType("java.lang.Long");
	retrainlooptime.setName("recommenderLoopTime");
	retrainlooptime.setValue("100");
	context.getNamingResources().addEnvironment(retrainlooptime);

	testTomcat.start();

	try {
		Thread.sleep(5000);
	} catch (InterruptedException e) {
	}
}
 
Example 10
Source File: TomcatTestHandler.java    From TeaStore with Apache License 2.0 4 votes vote down vote up
/**
 * Create a Tomcat test handler for persistence testing.
 * @param count Number of testing tomcats.
 * @param startPort Port to start with (do not use 0 for auto-assigning).
 * @param wireMockRule Wire mock rule for mocking the registry.The test handler will
 * add all services with respective stubs to the rule.
 * @param endpoints Class objects for the endpoints.
 * @throws ServletException Exception on failure.
 * @throws LifecycleException Exception on failure.
 * @throws JsonProcessingException Exception on failure.
 */
public TomcatTestHandler(int count, int startPort, WireMockRule wireMockRule, Class<?>... endpoints)
		throws ServletException, LifecycleException, JsonProcessingException {
	tomcats = new Tomcat[count];
	EMFManagerInitializer.initializeEMF();
	for (int i = 0; i < count; i++) {
		tomcats[i] = new Tomcat();
		tomcats[i].setPort(startPort + i);
		tomcats[i].setBaseDir(testWorkingDir);
		Context context = tomcats[i].addWebapp(CONTEXT, testWorkingDir);
		//Registry
		if (wireMockRule != null) {
			ContextEnvironment registryURL = new ContextEnvironment();
			registryURL.setDescription("");
			registryURL.setOverride(false);
			registryURL.setType("java.lang.String");
			registryURL.setName("registryURL");
			registryURL.setValue("http://localhost:" + wireMockRule.port()
			+ "/tools.descartes.teastore.registry/rest/services/");
			context.getNamingResources().addEnvironment(registryURL);
			ContextEnvironment servicePort = new ContextEnvironment();
			servicePort.setDescription("");
			servicePort.setOverride(false);
			servicePort.setType("java.lang.String");
		    servicePort.setName("servicePort");
		    servicePort.setValue("" + startPort + i);
			context.getNamingResources().addEnvironment(servicePort);
			context.addApplicationListener(RegistrationDaemon.class.getName());
		}
		//REST endpoints
		ResourceConfig restServletConfig = new ResourceConfig();
		for (Class<?> endpoint: endpoints) {
			restServletConfig.register(endpoint);
		}
		ServletContainer restServlet = new ServletContainer(restServletConfig);
		tomcats[i].addServlet(CONTEXT, "restServlet", restServlet);
		context.addServletMappingDecoded("/rest/*", "restServlet");
		tomcats[i].start();
	}
	if (wireMockRule != null) {
		initializeMockRegistry(wireMockRule, count, startPort);
	}
	System.out.println("Initializing Database with size " + CategoryRepository.REPOSITORY.getAllEntities().size());
}
 
Example 11
Source File: AbstractStoreRestTest.java    From TeaStore with Apache License 2.0 4 votes vote down vote up
/**
 * Sets up a store.
 * 
 * @throws Throwable
 *           Throws uncaught throwables for test to fail.
 */
@Before
public void setup() throws Throwable {
  BasicConfigurator.configure();

  storeTomcat = new Tomcat();
  storeTomcat.setPort(3000);
  storeTomcat.setBaseDir(testWorkingDir);
  storeTomcat.enableNaming();
  ContextEnvironment registryUrl3 = new ContextEnvironment();
  registryUrl3.setDescription("");
  registryUrl3.setOverride(false);
  registryUrl3.setType("java.lang.String");
  registryUrl3.setName("registryURL");
  registryUrl3
      .setValue("http://localhost:18080/tools.descartes.teastore.registry/rest/services/");
  Context context3 = storeTomcat.addWebapp("/tools.descartes.teastore.auth", testWorkingDir);
  context3.getNamingResources().addEnvironment(registryUrl3);
  ContextEnvironment servicePort3 = new ContextEnvironment();
  servicePort3.setDescription("");
  servicePort3.setOverride(false);
  servicePort3.setType("java.lang.String");
  servicePort3.setName("servicePort");
  servicePort3.setValue("3000");
  context3.getNamingResources().addEnvironment(servicePort3);
  ResourceConfig restServletConfig3 = new ResourceConfig();
  restServletConfig3.register(AuthCartRest.class);
  restServletConfig3.register(AuthUserActionsRest.class);
  ServletContainer restServlet3 = new ServletContainer(restServletConfig3);
  storeTomcat.addServlet("/tools.descartes.teastore.auth", "restServlet", restServlet3);
  context3.addServletMappingDecoded("/rest/*", "restServlet");
  context3.addApplicationListener(EmptyAuthStartup.class.getName());

  // Mock registry
  List<String> strings = new LinkedList<String>();
  strings.add("localhost:18080");
  String json = new ObjectMapper().writeValueAsString(strings);
  List<String> strings2 = new LinkedList<String>();
  strings2.add("localhost:3000");
  String json2 = new ObjectMapper().writeValueAsString(strings2);
  wireMockRule.stubFor(get(urlEqualTo(
      "/tools.descartes.teastore.registry/rest/services/" + Service.IMAGE.getServiceName() + "/"))
          .willReturn(okJson(json)));
  wireMockRule.stubFor(get(urlEqualTo(
      "/tools.descartes.teastore.registry/rest/services/" + Service.AUTH.getServiceName() + "/"))
          .willReturn(okJson(json2)));
  wireMockRule.stubFor(
      WireMock.put(WireMock.urlMatching("/tools.descartes.teastore.registry/rest/services/"
          + Service.AUTH.getServiceName() + "/.*")).willReturn(okJson(json2)));
  wireMockRule.stubFor(
      WireMock.delete(WireMock.urlMatching("/tools.descartes.teastore.registry/rest/services/"
          + Service.AUTH.getServiceName() + "/.*")).willReturn(okJson(json2)));
  wireMockRule.stubFor(get(urlEqualTo("/tools.descartes.teastore.registry/rest/services/"
      + Service.PERSISTENCE.getServiceName() + "/")).willReturn(okJson(json)));
  wireMockRule.stubFor(get(urlEqualTo("/tools.descartes.teastore.registry/rest/services/"
      + Service.RECOMMENDER.getServiceName() + "/")).willReturn(okJson(json)));

  // Mock images
  HashMap<String, String> img = new HashMap<>();
  img.put("andreBauer", "andreBauer");
  img.put("johannesGrohmann", "johannesGrohmann");
  img.put("joakimKistowski", "joakimKistowski");
  img.put("simonEismann", "simonEismann");
  img.put("norbertSchmitt", "norbertSchmitt");
  img.put("descartesLogo", "descartesLogo");
  img.put("icon", "icon");
  mockValidPostRestCall(img, "/tools.descartes.teastore.image/rest/image/getWebImages");

  storeTomcat.start();
}