Example usage for org.apache.commons.pool BasePoolableObjectFactory BasePoolableObjectFactory

List of usage examples for org.apache.commons.pool BasePoolableObjectFactory BasePoolableObjectFactory

Introduction

In this page you can find the example usage for org.apache.commons.pool BasePoolableObjectFactory BasePoolableObjectFactory.

Prototype

BasePoolableObjectFactory

Source Link

Usage

From source file:echowebserver.EchoWSData.java

public PoolableObjectFactory getPoolableObjectFactory() {
    return new BasePoolableObjectFactory() {
        public Object makeObject() {
            return new EchoWSData();
        }//w  ww. j  av  a  2 s .  c  o m

        public void passivateObject(Object obj) {
            EchoWSData ed = (EchoWSData) obj;
            ed.clean();
        }

        public void destroyObject(Object obj) {
            if (obj == null)
                return;
            passivateObject(obj);
            obj = null;
        }

        public boolean validateObject(Object obj) {
            if (obj == null)
                return false;
            else
                return true;
        }
    };
}

From source file:forwardserver.ForwardData.java

public PoolableObjectFactory getPoolableObjectFactory() {
    return new BasePoolableObjectFactory() {
        public Object makeObject() {
            return new ForwardData();//
        }/*from   w ww . jav a  2s .com*/

        public void passivateObject(Object obj) {
            ForwardData ed = (ForwardData) obj;
            ed.clean();
        }

        public void destroyObject(Object obj) {
            if (obj == null)
                return;
            passivateObject(obj);
            obj = null;
        }

        public boolean validateObject(Object obj) {
            if (obj == null) //
                return false;
            else
                return true;
        }
    };
}

From source file:filesrv.Data.java

public PoolableObjectFactory getPoolableObjectFactory() {
    return new BasePoolableObjectFactory() {
        public Object makeObject() {
            return new Data();
        }//  ww w .  j  a v a 2  s.  c o  m

        public void passivateObject(Object obj) {
            Data d = (Data) obj;
            d.clean();
        }

        public void destroyObject(Object obj) {
            if (obj == null)
                return;
            passivateObject(obj);
            obj = null;
        }

        public boolean validateObject(Object obj) {
            if (obj == null)
                return false;
            else
                return true;
        }
    };
}

From source file:de.escidoc.core.common.business.fedora.FedoraUtility.java

/**
 * See Interface for functional description.
 * //w  w  w .  j a  v a 2s.  c o m
 * @see InitializingBean #afterPropertiesSet()
 */
@Override
public void afterPropertiesSet() throws IOException, MalformedURLException, ServiceException {

    this.fedoraClientPool = new StackObjectPool(
            PoolUtils.synchronizedPoolableFactory(new BasePoolableObjectFactory() {
                /**
                 * See Interface for functional description.
                 * 
                 * @return
                 * @see BasePoolableObjectFactory #makeObject()
                 */
                @Override
                public Object makeObject() throws MalformedURLException {
                    return new FedoraClient(FedoraUtility.this.fedoraUrl, FedoraUtility.this.fedoraUser,
                            FedoraUtility.this.fedoraPassword);
                }
            }), MAX_IDLE, INIT_IDLE_CAPACITY);

    this.apiaPool = new StackObjectPool(PoolUtils.synchronizedPoolableFactory(new BasePoolableObjectFactory() {
        /**
         * See Interface for functional description.
         * 
         * @return
         * @see BasePoolableObjectFactory #makeObject()
         */
        @Override
        public Object makeObject() throws IOException, MalformedURLException, ServiceException {
            return new FedoraClient(FedoraUtility.this.fedoraUrl, FedoraUtility.this.fedoraUser,
                    FedoraUtility.this.fedoraPassword).getAPIA();
        }
    }), MAX_IDLE, INIT_IDLE_CAPACITY);

    this.apimPool = new StackObjectPool(new BasePoolableObjectFactory() {
        /**
         * See Interface for functional description.
         * 
         * @return
         * @see BasePoolableObjectFactory #makeObject()
         */
        @Override
        public Object makeObject() throws IOException, MalformedURLException, ServiceException {
            return new FedoraClient(FedoraUtility.this.fedoraUrl, FedoraUtility.this.fedoraUser,
                    FedoraUtility.this.fedoraPassword).getAPIM();
        }
    }, MAX_IDLE, INIT_IDLE_CAPACITY);

    this.syncRestQuery = this.fedoraUrl + "/risearch?flush=true";
}

From source file:org.apache.wookie.beans.jcr.JCRPersistenceManager.java

/**
 * Initialize implementation with configuration.
 * // w ww  . j  a v a  2  s . c o  m
 * @param configuration configuration properties
 * @param initializeStore truncate and initialize persistent store
 */
public static void initialize(Configuration configuration, boolean initializeStore) {
    try {
        // configuration
        repositoryUser = configuration.getString(PERSISTENCE_MANAGER_USER_PROPERTY_NAME);
        repositoryPassword = configuration.getString(PERSISTENCE_MANAGER_PASSWORD_PROPERTY_NAME);
        repositoryWorkspace = configuration.getString(PERSISTENCE_MANAGER_WORKSPACE_PROPERTY_NAME);
        rootPath = configuration.getString(PERSISTENCE_MANAGER_ROOT_PATH_PROPERTY_NAME);
        for (Map.Entry<Class<? extends IBean>, Class<? extends IBean>> mapping : BEAN_INTERFACE_TO_CLASS_MAP
                .entrySet()) {
            Class<? extends IBean> beanClass = mapping.getValue();
            Class<? extends IBean> beanInterface = mapping.getKey();
            String name = beanInterface.getSimpleName();
            if (name.startsWith("I")) {
                name = name.substring(1);
            }
            if (!name.endsWith("s")) {
                name = name + "s";
            }
            String nodeRootPath = rootPath + "/" + name;
            beanClassNodeRootPaths.put(beanClass, nodeRootPath);
        }

        // create JCR credentials session pool
        PoolableObjectFactory sessionFactory = new BasePoolableObjectFactory() {
            /* (non-Javadoc)
             * @see org.apache.commons.pool.BasePoolableObjectFactory#passivateObject(java.lang.Object)
             */
            public void passivateObject(Object obj) throws Exception {
                // clear OCM object cache
                ((ObjectContentManagerImpl) obj).setRequestObjectCache(new RequestObjectCacheImpl());
            }

            /* (non-Javadoc)
             * @see org.apache.commons.pool.BasePoolableObjectFactory#makeObject()
             */
            public Object makeObject() throws Exception {
                // lookup JCR repository from context
                Context initialContext = new InitialContext();
                Repository repository = (Repository) initialContext
                        .lookup(WIDGET_REPOSITORY_JNDI_REPOSITORY_FULL_NAME);

                // create and login JCR session
                Credentials credentials = new SimpleCredentials(repositoryUser,
                        repositoryPassword.toCharArray());
                Session session = ((repositoryWorkspace != null)
                        ? repository.login(credentials, repositoryWorkspace)
                        : repository.login(credentials));

                // return session object content manager for session
                return new SessionObjectContentManagerImpl(session, new AnnotationMapperImpl(CLASS_LIST));
            }

            /* (non-Javadoc)
             * @see org.apache.commons.pool.BasePoolableObjectFactory#destroyObject(java.lang.Object)
             */
            public void destroyObject(Object obj) throws Exception {
                // logout and close object content manager and session
                ((ObjectContentManagerImpl) obj).logout();
            }
        };
        ocmPool = new GenericObjectPool(sessionFactory, 0, GenericObjectPool.WHEN_EXHAUSTED_GROW, 0, 5);
        ocmPool.setTimeBetweenEvictionRunsMillis(60000);
        ocmPool.setMinEvictableIdleTimeMillis(300000);

        // initialize persistent store
        if (initializeStore) {
            // borrow object content manager and initialization session from pool
            ObjectContentManager ocm = (ObjectContentManager) ocmPool.borrowObject();
            Session session = ocm.getSession();

            // initialize root path in repository

            // Jackrabbit/JCR 2.X
            //boolean rootPathNodeExists = session.nodeExists(rootPath);

            // Jackrabbit/JCR 1.X
            boolean rootPathNodeExists = session.itemExists(rootPath);

            if (rootPathNodeExists) {
                // delete nodes of root path node

                // Jackrabbit/JCR 2.X
                //Node rootNode = session.getNode(rootPath);

                // Jackrabbit/JCR 1.X
                Node rootNode = (Node) session.getItem(rootPath);

                NodeIterator nodesIter = rootNode.getNodes();
                while (nodesIter.hasNext()) {
                    nodesIter.nextNode().remove();
                }
            } else {
                // create unstructured node hierarchy
                int rootPathParentIndex = -1;
                int rootPathIndex = rootPath.indexOf('/', 1);
                while (rootPathIndex != -1) {
                    // Jackrabbit/JCR 2.X
                    //Node parentNode = session.getNode(rootPath.substring(0, ((rootPathParentIndex != -1) ? rootPathParentIndex : 1)));

                    // Jackrabbit/JCR 1.X
                    Node parentNode = (Node) session.getItem(
                            rootPath.substring(0, ((rootPathParentIndex != -1) ? rootPathParentIndex : 1)));

                    String nodeName = rootPath.substring(
                            ((rootPathParentIndex != -1) ? rootPathParentIndex + 1 : 1), rootPathIndex);
                    parentNode.addNode(nodeName, "nt:unstructured");
                    rootPathParentIndex = rootPathIndex;
                    rootPathIndex = rootPath.indexOf('/', rootPathIndex + 1);
                }

                // Jackrabbit/JCR 2.X
                //Node parentNode = session.getNode(rootPath.substring(0, ((rootPathParentIndex != -1) ? rootPathParentIndex : 1)));

                // Jackrabbit/JCR 1.X
                Node parentNode = (Node) session.getItem(
                        rootPath.substring(0, ((rootPathParentIndex != -1) ? rootPathParentIndex : 1)));

                String nodeName = rootPath
                        .substring(((rootPathParentIndex != -1) ? rootPathParentIndex + 1 : 1));
                parentNode.addNode(nodeName, "nt:unstructured");
            }
            // create bean class node root paths

            // Jackrabbit/JCR 2.X
            //Node rootNode = session.getNode(rootPath);

            // Jackrabbit/JCR 1.X
            Node rootNode = (Node) session.getItem(rootPath);

            for (String nodeRootPath : beanClassNodeRootPaths.values()) {
                String nodeName = nodeRootPath.substring(rootPath.length() + 1);
                rootNode.addNode(nodeName, "nt:unstructured");
            }
            session.save();

            // register/reregister repository node types
            NodeTypeManager nodeTypeManager = session.getWorkspace().getNodeTypeManager();
            InputStream nodeTypesCNDStream = JCRPersistenceManager.class
                    .getResourceAsStream("wookie-schema.cnd");
            if (nodeTypesCNDStream == null) {
                throw new IllegalArgumentException(
                        "Unable to load node types configuration: wookie-schema.cnd");
            }

            // Jackrabbit/JCR 2.X
            //Reader nodeTypesCNDReader = new InputStreamReader(nodeTypesCNDStream);
            //NamespaceRegistry namespaceRegistry = session.getWorkspace().getNamespaceRegistry();
            //ValueFactory valueFactory = session.getValueFactory();
            //CndImporter.registerNodeTypes(nodeTypesCNDReader, "wookie-schema.cnd", nodeTypeManager, namespaceRegistry, valueFactory, true);

            // Jackrabbit/JCR 1.X
            ((NodeTypeManagerImpl) nodeTypeManager).registerNodeTypes(nodeTypesCNDStream,
                    NodeTypeManagerImpl.TEXT_X_JCR_CND, true);

            // save session used to load node types
            session.save();
            logger.info("Persistent store initialized at " + rootPath);

            // return object content manager and initialization session to pool
            ocmPool.returnObject(ocm);
        }

        logger.info("Initialized");
    } catch (Exception e) {
        throw new RuntimeException("Unable to initialize: " + e, e);
    }
}

From source file:org.openrepose.common.auth.ResponseUnmarshaller.java

public ResponseUnmarshaller(JAXBContext jaxbContext) {
    this.jaxbContext = jaxbContext;
    objectPool = new SoftReferenceObjectPool<>(new BasePoolableObjectFactory<Unmarshaller>() {

        @Override//from w ww. ja  va 2s  . c o m
        public Unmarshaller makeObject() {
            try {
                return ResponseUnmarshaller.this.jaxbContext.createUnmarshaller();
            } catch (JAXBException ex) {
                throw new ResourceConstructionException("Unable to build jaxb unmarshaller", ex);
            }
        }
    });
}

From source file:org.openrepose.commons.utils.transform.jaxb.AbstractJaxbTransform.java

public AbstractJaxbTransform(JAXBContext ctx) {
    jaxbContext = ctx;/*from  www  .  java2 s.  com*/

    marshallerPool = new SoftReferenceObjectPool<>(new BasePoolableObjectFactory<Marshaller>() {
        @Override
        public Marshaller makeObject() {
            try {
                return jaxbContext.createMarshaller();
            } catch (JAXBException jaxbe) {
                throw new ResourceConstructionException(jaxbe.getMessage(), jaxbe);
            }
        }
    });

    unmarshallerPool = new SoftReferenceObjectPool<>(new BasePoolableObjectFactory<Unmarshaller>() {
        @Override
        public Unmarshaller makeObject() {
            try {
                return jaxbContext.createUnmarshaller();
            } catch (JAXBException jaxbe) {
                throw new ResourceConstructionException(jaxbe.getMessage(), jaxbe);
            }
        }
    });
}

From source file:org.openrepose.commons.utils.transform.xslt.AbstractXslTransform.java

public AbstractXslTransform(Templates transformTemplates) {
    this.transformationTemplates = transformTemplates;

    xsltResourcePool = new SoftReferenceObjectPool<>(new BasePoolableObjectFactory<Transformer>() {

        @Override//from   w  w  w.  j a  v a  2  s  .  c o m
        public Transformer makeObject() {
            try {
                return transformationTemplates.newTransformer();
            } catch (TransformerConfigurationException configurationException) {
                throw new XsltTransformationException(
                        "Failed to generate XSLT transformer. Reason: " + configurationException.getMessage(),
                        configurationException);
            }
        }
    });
}

From source file:org.openrepose.commons.utils.transform.xslt.XsltTransformConstruction.java

public ObjectPool<Transformer> generateXsltResourcePool(final Templates transformationTemplates) {
    return new SoftReferenceObjectPool<>(new BasePoolableObjectFactory<Transformer>() {

        @Override//from  w w  w . j  av a2  s  .co m
        public Transformer makeObject() {
            try {
                return transformationTemplates.newTransformer();
            } catch (TransformerConfigurationException configurationException) {
                throw new XsltTransformationException(
                        "Failed to generate XSLT transformer. Reason: " + configurationException.getMessage(),
                        configurationException);
            }
        }
    });
}

From source file:org.siphon.db2js.jshttp.ServerUnitManager.java

public JsEngineHandlerContext getEngineContext(final String srcFile, final String aliasPath,
        final DataSource dataSource, final Map<String, Object> otherArgs) throws Exception {

    File file = null;/*from  w ww. ja  v a2s.  c  o  m*/
    if (contexts.containsKey(srcFile)) {
        ObjectPool<JsEngineHandlerContext> pool = contexts.get(srcFile);
        JsEngineHandlerContext ctxt = pool.borrowObject();
        ctxt.setPool(pool);
        return ctxt;
    } else if ((file = new File(srcFile)).exists()) {
        ObjectPool<JsEngineHandlerContext> enginePool;
        BasePoolableObjectFactory<JsEngineHandlerContext> factory = new BasePoolableObjectFactory<JsEngineHandlerContext>() {

            @Override
            public JsEngineHandlerContext makeObject() throws Exception {
                return createEngineContext(srcFile, aliasPath, dataSource, otherArgs);
            }
        };

        enginePool = new GenericObjectPool<JsEngineHandlerContext>(factory);

        JsEngineHandlerContext ctxt = enginePool.borrowObject();
        ctxt.setPool(enginePool);

        contexts.put(srcFile, enginePool);

        return ctxt;
    } else {
        throw new IOException("file not found " + srcFile);
    }
}