KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jdo > spi > persistence > support > sqlstore > ejb > DeploymentHelper


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * DeploymentHelper.java
26  *
27  * Created on September 30, 2003.
28  */

29
30 package com.sun.jdo.spi.persistence.support.sqlstore.ejb;
31
32 import java.sql.Connection JavaDoc;
33 import java.sql.SQLException JavaDoc;
34 import java.util.Properties JavaDoc;
35 import java.util.ResourceBundle JavaDoc;
36
37 import javax.sql.DataSource JavaDoc;
38 import javax.naming.InitialContext JavaDoc;
39 import javax.naming.NamingException JavaDoc;
40
41 import com.sun.jdo.api.persistence.support.JDOFatalUserException;
42 import com.sun.jdo.api.persistence.support.PersistenceManagerFactory;
43 import com.sun.jdo.spi.persistence.support.sqlstore.LogHelperPersistenceManager;
44
45 import com.sun.jdo.spi.persistence.utility.I18NHelper;
46 import com.sun.jdo.spi.persistence.utility.StringHelper;
47 import com.sun.jdo.spi.persistence.utility.database.DatabaseConstants;
48 import com.sun.jdo.spi.persistence.utility.logging.Logger;
49
50 import com.sun.enterprise.deployment.Application;
51 import com.sun.enterprise.deployment.BundleDescriptor;
52 import com.sun.enterprise.deployment.EjbBundleDescriptor;
53 import com.sun.enterprise.connectors.ConnectorRuntime;
54
55 /**
56  * This class is used for static method invocations to avoid unnecessary
57  * registration requirements to use EJBHelper and/or CMPHelper from
58  * deploytool, verifier, or any other stand-alone client.
59  *
60  */

61 public class DeploymentHelper
62     {
63
64     /**
65      * Default DDL name prefix. Need to have something to avoid
66      * generating hidden names when a suffix is added to an empty string.
67      * E.g. <code>.dbschema</code> name can be difficult to find,
68      * while <code>default.dbschema</code> will signal that the default
69      * had been used.
70      **/

71     private final static String JavaDoc DEFAULT_NAME = "default"; // NOI18N
72

73     /** I18N message handler */
74     private final static ResourceBundle JavaDoc messages = I18NHelper.loadBundle(
75         "com.sun.jdo.spi.persistence.support.sqlstore.Bundle", // NOI18N
76
DeploymentHelper.class.getClassLoader());
77
78     /** The logger */
79     private static Logger logger = LogHelperPersistenceManager.getLogger();
80
81     /**
82      * Returns name prefix for DDL files extracted from the info instance by the
83      * Sun-specific code.
84      *
85      * @param info the instance to use for the name generation.
86      * @return name prefix as String.
87      */

88     public static String JavaDoc getDDLNamePrefix(Object JavaDoc info) {
89         StringBuffer JavaDoc rc = new StringBuffer JavaDoc();
90
91         if (info instanceof BundleDescriptor) {
92             BundleDescriptor bundle = (BundleDescriptor)info;
93             rc.append(bundle.getApplication().getRegistrationName());
94
95             Application application = bundle.getApplication();
96             if (!application.isVirtual()) {
97                 String JavaDoc modulePath = bundle.getModuleDescriptor().getArchiveUri();
98                 int l = modulePath.length();
99
100                 // Remove ".jar" from the module's jar name.
101
rc.append(DatabaseConstants.NAME_SEPARATOR).
102                     append(modulePath.substring(0, l - 4));
103             }
104
105         } // no other option is available at this point.
106

107         return (rc.length() == 0)? DEFAULT_NAME : rc.toString();
108     }
109
110     /**
111      * Returns javatodb flag for this EjbBundleDescriptor
112      * @param bundle a EjbBundleDescriptor
113      * @return true if there is a property entry associated with the
114      * corresponding cmp-resource element, which contains "true" as
115      * the value for the <code>DatabaseConstants.JAVA_TO_DB_FLAG</code>
116      * key.
117      */

118     public static boolean isJavaToDatabase(EjbBundleDescriptor bundle) {
119         Properties JavaDoc userPolicy = bundle.getCMPResourceReference()
120                 .getSchemaGeneratorProperties();
121         return isJavaToDatabase(userPolicy);
122     }
123
124     /**
125      * Returns boolean value for the <code>DatabaseConstants.JAVA_TO_DB_FLAG</code>
126      * flag in this Properties object.
127      * @param prop a Properties object where flag is located
128      * @return true if there is a property value that contains "true" as
129      * the value for the <code>DatabaseConstants.JAVA_TO_DB_FLAG</code>
130      * key.
131      */

132     public static boolean isJavaToDatabase(Properties JavaDoc prop) {
133         if (prop != null) {
134             String JavaDoc value = prop.getProperty(DatabaseConstants.JAVA_TO_DB_FLAG);
135             if (! StringHelper.isEmpty(value)) {
136                  if (logger.isLoggable(Logger.FINE))
137                      logger.fine(DatabaseConstants.JAVA_TO_DB_FLAG + " property is set."); // NOI18N
138
return Boolean.valueOf(value).booleanValue();
139             }
140         }
141         return false;
142     }
143
144     /** Get a Connection from the resource specified by the JNDI name
145      * of a CMP resource.
146      * This connection is aquired from a special PM resource which allows
147      * to use its connections outside of a business method invocation.
148      * The deployment processing is required to use only those connections.
149      *
150      * @param name JNDI name of a cmp-resource for the connection.
151      * @return a Connection.
152      * @throws JDOFatalUserException if name cannot be looked up, or we
153      * cannot get a connection based on the name.
154      * @throws SQLException if can not get a Connection.
155      */

156     public static Connection JavaDoc getConnection(String JavaDoc name) throws SQLException JavaDoc {
157         if (logger.isLoggable(logger.FINE)) {
158             logger.fine("ejb.DeploymentHelper.getconnection", name); //NOI18N
159
}
160         return ConnectorRuntime.getRuntime().getConnection(name);
161     }
162
163     /** Create a RuntimeException for unexpected instance returned
164      * from JNDI lookup.
165      *
166      * @param name the JNDI name that had been looked up.
167      * @param value the value returned from the JNDI lookup.
168      * @throws JDOFatalUserException.
169      */

170     private static void handleUnexpectedInstance(String JavaDoc name, Object JavaDoc value) {
171         RuntimeException JavaDoc e = new JDOFatalUserException(
172                 I18NHelper.getMessage(messages,
173                         "ejb.jndi.unexpectedinstance", //NOI18N
174
name, value.getClass().getName()));
175         logger.severe(e.toString());
176  
177         throw e;
178  
179     }
180 }
181
Popular Tags