alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Glassfish example source code file (DeploymentHelper.java)

This example Glassfish source code file (DeploymentHelper.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Glassfish tags/keywords

connection, datasource, deploymenthelper, habitat, jdbc, jdofataluserexception, jdofataluserexception, naming, noi18n, noi18n, object, object, properties, sql, sqlexception, string, string, util

The Glassfish DeploymentHelper.java source code

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright (c) 1997-2010 Oracle and/or its affiliates. All rights reserved.
 *
 * The contents of this file are subject to the terms of either the GNU
 * General Public License Version 2 only ("GPL") or the Common Development
 * and Distribution License("CDDL") (collectively, the "License").  You
 * may not use this file except in compliance with the License.  You can
 * obtain a copy of the License at
 * https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
 * or packager/legal/LICENSE.txt.  See the License for the specific
 * language governing permissions and limitations under the License.
 *
 * When distributing the software, include this License Header Notice in each
 * file and include the License file at packager/legal/LICENSE.txt.
 *
 * GPL Classpath Exception:
 * Oracle designates this particular file as subject to the "Classpath"
 * exception as provided by Oracle in the GPL Version 2 section of the License
 * file that accompanied this code.
 *
 * Modifications:
 * If applicable, add the following below the License Header, with the fields
 * enclosed by brackets [] replaced by your own identifying information:
 * "Portions Copyright [year] [name of copyright owner]"
 *
 * Contributor(s):
 * If you wish your version of this file to be governed by only the CDDL or
 * only the GPL Version 2, indicate your decision by adding "[Contributor]
 * elects to include this software in this distribution under the [CDDL or GPL
 * Version 2] license."  If you don't indicate a single choice of license, a
 * recipient has the option to distribute your version of this file under
 * either the CDDL, the GPL Version 2 or to extend the choice of license to
 * its licensees as provided above.  However, if you add GPL Version 2 code
 * and therefore, elected the GPL Version 2 license, then the option applies
 * only if the new code is made subject to such option by the copyright
 * holder.
 */

/*
 * DeploymentHelper.java
 *
 * Created on September 30, 2003.
 */

package com.sun.jdo.spi.persistence.support.sqlstore.ejb;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.NamingException;

import com.sun.jdo.api.persistence.support.JDOFatalUserException;
import com.sun.jdo.api.persistence.support.PersistenceManagerFactory;
import com.sun.jdo.spi.persistence.support.sqlstore.LogHelperPersistenceManager;

import org.glassfish.persistence.common.I18NHelper;
import org.glassfish.persistence.common.Java2DBProcessorHelper;
import com.sun.jdo.spi.persistence.utility.StringHelper;
import com.sun.jdo.spi.persistence.utility.logging.Logger;
import org.glassfish.persistence.common.DatabaseConstants;

import com.sun.enterprise.deployment.Application;
import com.sun.enterprise.deployment.EjbBundleDescriptor;
import com.sun.appserv.connectors.internal.api.ConnectorRuntime;
import org.glassfish.internal.api.Globals;
import org.jvnet.hk2.component.Habitat;

/** 
 * This class is used for static method invocations to avoid unnecessary
 * registration requirements to use EJBHelper and/or CMPHelper from 
 * deploytool, verifier, or any other stand-alone client.
 * 
 */
public class DeploymentHelper
    {

    /** I18N message handler */
    private final static ResourceBundle messages = I18NHelper.loadBundle(
        "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", // NOI18N
        DeploymentHelper.class.getClassLoader());

    /** The logger */
    private static Logger logger = LogHelperPersistenceManager.getLogger();

    /** 
     * Returns name prefix for DDL files extracted from the info instance by the
     * Sun-specific code.
     *   
     * @param info the instance to use for the name generation.
     * @return name prefix as String. 
     */   
    public static String getDDLNamePrefix(Object info) { 
        return Java2DBProcessorHelper.getDDLNamePrefix(info);
    }

    /**
     * Returns javatodb flag for this EjbBundleDescriptor
     * @param bundle a EjbBundleDescriptor
     * @return true if there is a property entry associated with the
     * corresponding cmp-resource element, which contains "true" as
     * the value for the <code>DatabaseConstants.JAVA_TO_DB_FLAG
     * key.
     */  
    public static boolean isJavaToDatabase(EjbBundleDescriptor bundle) {
        Properties userPolicy = bundle.getCMPResourceReference()
                .getSchemaGeneratorProperties();
        return isJavaToDatabase(userPolicy);
    }

    /**
     * Returns boolean value for the <code>DatabaseConstants.JAVA_TO_DB_FLAG
     * flag in this Properties object.
     * @param prop a Properties object where flag is located
     * @return true if there is a property value that contains "true" as
     * the value for the <code>DatabaseConstants.JAVA_TO_DB_FLAG
     * key.
     */  
    public static boolean isJavaToDatabase(Properties prop) {
        if (prop != null) {
            String value = prop.getProperty(DatabaseConstants.JAVA_TO_DB_FLAG);
            if (! StringHelper.isEmpty(value)) {
                 if (logger.isLoggable(Logger.FINE))
                     logger.fine(DatabaseConstants.JAVA_TO_DB_FLAG + " property is set."); // NOI18N
                 return Boolean.valueOf(value).booleanValue();
            }
        }
        return false;
    }

    /** Get a Connection from the resource specified by the JNDI name 
     * of a CMP resource.
     * This connection is aquired from a non-transactional resource which does not 
     * go through transaction enlistment/delistment.
     * The deployment processing is required to use only those connections.
     *
     * @param name JNDI name of a cmp-resource for the connection.
     * @return a Connection.
     * @throws JDOFatalUserException if name cannot be looked up, or we
     * cannot get a connection based on the name.
     * @throws SQLException if can not get a Connection.
     */  
    public static Connection getConnection(String name) throws SQLException {
        if (logger.isLoggable(logger.FINE)) {
            logger.fine("ejb.DeploymentHelper.getconnection", name); //NOI18N
        }

        // TODO - pass Habitat or ConnectorRuntime as an argument.

        Habitat habitat = Globals.getDefaultHabitat();
        DataSource ds = null;
        try {
            ConnectorRuntime connectorRuntime = habitat.getByContract(ConnectorRuntime.class);
            ds = DataSource.class.cast(connectorRuntime.lookupNonTxResource(name, true));
        } catch (Exception e) { 
            throw new JDOFatalUserException(
                I18NHelper.getMessage(messages,
                        "ejb.jndi.lookupfailed", name)); //NOI18N
        }
        return ds.getConnection();
    }    

    /** Create a RuntimeException for unexpected instance returned
     * from JNDI lookup.
     *
     * @param name the JNDI name that had been looked up.
     * @param value the value returned from the JNDI lookup.
     * @throws JDOFatalUserException.
     */
    private static void handleUnexpectedInstance(String name, Object value) {
        RuntimeException e = new JDOFatalUserException(
                I18NHelper.getMessage(messages,
                        "ejb.jndi.unexpectedinstance", //NOI18N
                        name, value.getClass().getName()));
        logger.severe(e.toString());
 
        throw e;
 
    }
}

Other Glassfish examples (source code examples)

Here is a short list of links related to this Glassfish DeploymentHelper.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.