Example usage for javax.servlet ServletContext hashCode

List of usage examples for javax.servlet ServletContext hashCode

Introduction

In this page you can find the example usage for javax.servlet ServletContext hashCode.

Prototype

@HotSpotIntrinsicCandidate
public native int hashCode();

Source Link

Document

Returns a hash code value for the object.

Usage

From source file:com.ecyrd.jspwiki.WikiEngine.java

/**
 *  Gets a WikiEngine related to the servlet. Works just like getInstance( ServletConfig )
 *
 *  @param context The ServletContext of the webapp servlet/JSP calling this method.
 *  @param props  A set of properties, or null, if we are to load JSPWiki's default
 *                jspwiki.properties (this is the usual case).
 *
 *  @return One fully functional, properly behaving WikiEngine.
 *  @throws InternalWikiException If the WikiEngine instantiation fails.
 *//*from w w  w.  j  a v  a2  s  .c om*/

// FIXME: Potential make-things-easier thingy here: no need to fetch the wikiengine anymore
//        Wiki.jsp.jspInit() [really old code]; it's probably even faster to fetch it
//        using this method every time than go to pageContext.getAttribute().

public static synchronized WikiEngine getInstance(ServletContext context, Properties props)
        throws InternalWikiException {
    WikiEngine engine = (WikiEngine) context.getAttribute(ATTR_WIKIENGINE);

    if (engine == null) {
        String appid = Integer.toString(context.hashCode()); //FIXME: Kludge, use real type.

        context.log(" Assigning new engine to " + appid);
        try {
            if (props == null) {
                props = PropertyReader.loadWebAppProps(context);
            }

            engine = new WikiEngine(context, appid, props);
            context.setAttribute(ATTR_WIKIENGINE, engine);
        } catch (Exception e) {
            context.log("ERROR: Failed to create a Wiki engine: " + e.getMessage());
            throw new InternalWikiException("No wiki engine, check logs.");
        }

    }

    return engine;
}

From source file:org.apache.wiki.WikiEngine.java

/**
 *  Gets a WikiEngine related to the servlet. Works just like getInstance( ServletConfig )
 *
 *  @param context The ServletContext of the webapp servlet/JSP calling this method.
 *  @param props  A set of properties, or null, if we are to load JSPWiki's default
 *                jspwiki.properties (this is the usual case).
 *
 *  @return One fully functional, properly behaving WikiEngine.
 *  @throws InternalWikiException If the WikiEngine instantiation fails.
 *///from  ww  w.jav  a 2s.  c  om

// FIXME: Potential make-things-easier thingy here: no need to fetch the wikiengine anymore
//        Wiki.jsp.jspInit() [really old code]; it's probably even faster to fetch it
//        using this method every time than go to pageContext.getAttribute().

public static synchronized WikiEngine getInstance(ServletContext context, Properties props)
        throws InternalWikiException {
    WikiEngine engine = (WikiEngine) context.getAttribute(ATTR_WIKIENGINE);

    if (engine == null) {
        String appid = Integer.toString(context.hashCode()); //FIXME: Kludge, use real type.

        context.log(" Assigning new engine to " + appid);
        try {
            if (props == null) {
                props = PropertyReader.loadWebAppProps(context);
            }

            engine = new WikiEngine(context, appid, props);
            context.setAttribute(ATTR_WIKIENGINE, engine);
        } catch (Exception e) {
            context.log("ERROR: Failed to create a Wiki engine: " + e.getMessage());
            log.error("ERROR: Failed to create a Wiki engine, stacktrace follows ", e);
            throw new InternalWikiException("No wiki engine, check logs.");
        }

    }

    return engine;
}

From source file:org.eclipse.equinox.http.servlet.tests.ServletTest.java

public void test_Listener10() throws Exception {
    BaseServletContextListener scl1 = new BaseServletContextListener();
    BaseServletContextListener scl2 = new BaseServletContextListener();
    BaseServletContextListener scl3 = new BaseServletContextListener();

    Dictionary<String, String> listenerProps = new Hashtable<String, String>();
    listenerProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER, "true");
    registrations.add(getBundleContext().registerService(ServletContextListener.class, scl1, listenerProps));

    listenerProps = new Hashtable<String, String>();
    listenerProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER, "true");
    registrations.add(getBundleContext().registerService(ServletContextListener.class, scl2, listenerProps));

    Dictionary<String, String> contextProps = new Hashtable<String, String>();
    contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME, "a");
    contextProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_PATH, "/a");
    registrations/* ww w .ja  v a2 s .  c o m*/
            .add(getBundleContext().registerService(ServletContextHelper.class, new ServletContextHelper() {
            }, contextProps));

    listenerProps = new Hashtable<String, String>();
    listenerProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_LISTENER, "true");
    listenerProps.put(HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_SELECT,
            "(" + HttpWhiteboardConstants.HTTP_WHITEBOARD_CONTEXT_NAME + "=a)");
    registrations.add(getBundleContext().registerService(ServletContextListener.class, scl3, listenerProps));

    ServletContext servletContext1 = scl1.servletContext;
    ServletContext servletContext2 = scl2.servletContext;
    ServletContext servletContext3 = scl3.servletContext;

    Assert.assertNotNull(servletContext1);
    Assert.assertNotNull(servletContext2);
    Assert.assertNotNull(servletContext3);

    Assert.assertTrue(servletContext1.equals(servletContext1));
    Assert.assertTrue(servletContext2.equals(servletContext2));
    Assert.assertTrue(servletContext3.equals(servletContext3));

    Assert.assertTrue(servletContext1.equals(servletContext2));
    Assert.assertFalse(servletContext1.equals(servletContext3));
    Assert.assertFalse(servletContext2.equals(servletContext3));

    // Asserts two invocations return the same value
    Assert.assertEquals(servletContext1.hashCode(), servletContext1.hashCode());
    Assert.assertEquals(servletContext2.hashCode(), servletContext2.hashCode());
    Assert.assertEquals(servletContext3.hashCode(), servletContext3.hashCode());

    Assert.assertEquals(servletContext1.hashCode(), servletContext2.hashCode());
    Assert.assertNotEquals(servletContext1.hashCode(), servletContext3.hashCode());
    Assert.assertNotEquals(servletContext2.hashCode(), servletContext3.hashCode());
}

From source file:org.seasar.velocity.tools.S2ServletToolboxManager.java

/**
 * ServletToolboxManager factory method.
 * This method will ensure there is exactly one ServletToolboxManager
 * per xml toolbox configuration file./*  www . j  a  va  2 s .c  o  m*/
 */
public static synchronized S2ServletToolboxManager getInstance(ServletContext servletContext,
        String toolboxFile) {
    // little fix up
    if (!toolboxFile.startsWith("/")) {
        toolboxFile = "/" + toolboxFile;
    }

    // get the unique key for this toolbox file in this servlet context
    String uniqueKey = servletContext.hashCode() + ':' + toolboxFile;

    // check if a previous instance exists
    S2ServletToolboxManager toolboxManager = (S2ServletToolboxManager) managersMap.get(uniqueKey);

    if (toolboxManager == null) {
        // if not, build one
        InputStream is = null;
        try {
            // get the bits
            is = servletContext.getResourceAsStream(toolboxFile);

            if (is != null) {
                LOG.info("Using config file '" + toolboxFile + "'");

                toolboxManager = new S2ServletToolboxManager(servletContext);
                toolboxManager.load(is);

                // remember it
                managersMap.put(uniqueKey, toolboxManager);

                LOG.debug("Toolbox setup complete.");
            } else {
                LOG.debug("No toolbox was found at '" + toolboxFile + "'");
            }
        } catch (Exception e) {
            LOG.error("Problem loading toolbox '" + toolboxFile + "'", e);
        } finally {
            try {
                if (is != null) {
                    is.close();
                }
            } catch (Exception ee) {
            }
        }
    }
    return toolboxManager;
}