Coder Social home page Coder Social logo

ggrandes / classloadermap Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 15 KB

When you want same "System Property" name, but with different value (by ClassLoader). ClassLoaderMap is Hierarchical Map associated to a ClassLoader.

License: Apache License 2.0

Java 100.00%
system-properties classloader java

classloadermap's Introduction

ClassLoaderMap

When you want same "System Property" name, but with different value by ClassLoader. ClassLoaderMap is Hierarchical Map associated to a ClassLoader. Open Source Java project under Apache License v2.0

Current Stable Version is 1.0.1


DOC

Usage Example

<!-- Context Listener for Servlet Container -->
<!-- tomcat/conf/web.xml or WEB-INF/web.xml -->
<listener>
	<description>Register "servlet.ContextPath" and "servlet.ContextName" in ClassLoaderMap</description>
	<listener-class>org.javastack.classloadermap.servlet.ClassLoaderMapContextListener</listener-class>
</listener>
// Sample ClassLoader Hierarchy (Tomcat 7)
//
//      Bootstrap
//          |
//       System
//          |
//       Common
//       /     \
//  Webapp1   Webapp2
//

/**
 * Generic Code
 */
public class ExampleConstants {
	public final String BASE_ID = "api.acme.com:";
}
public class ExampleID {
    private static final CTX_PROP = "servlet.ContextName";
	public String getAppID() {
	    // Usage 1
	    Class<?> ref = ExampleID.class;
	    // Usage 2
	    // ClassLoader ref = Thread.currentThread().getContextClassLoader();
	    String webAppName = ClassLoaderMap.get(ref, CTX_PROP);
		return ExampleConstants.BASE_ID +
			// get name in decoupled mode 
			(webAppName == null ? "standalone" : webAppName);
	}
}

/**
 * Tomcat Servlet
 */
public class ExampleServlet extends HttpServlet {
	@Override
	protected void doGet(final HttpServletRequest request, 
			final HttpServletResponse response)
			throws ServletException, IOException {
		final PrintWriter out = response.getWriter();
		out.println(new ExampleID().getAppID());
	}
}

// Output will be:
// api.acme.com:Webapp1
// api.acme.com:Webapp2

MAVEN

Add the dependency to your pom.xml:

<dependency>
    <groupId>org.javastack</groupId>
    <artifactId>classloadermap</artifactId>
    <version>1.0.1</version>
</dependency>
Note: If you want use ClassLoaderMapContextListener in global Tomcat web.xml (as infra-structure code), you can copy classloadermap-X.X.X.jar into ${CATALINA_HOME}/lib/

Inspired in ClassLoader, this code is Java-minimalistic version.

classloadermap's People

Contributors

ggrandes avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

plumpmath

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.