KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > ejb > base > sfsb > initialization > PersistenceStrategyBuilderFactory


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 package com.sun.ejb.base.sfsb.initialization;
25
26 import java.util.logging.Level JavaDoc;
27 import java.util.logging.Logger JavaDoc;
28
29 import com.sun.ejb.spi.sfsb.initialization.PersistenceStrategyBuilder;
30
31 import com.sun.logging.LogDomains;
32
33 /**
34  *
35  * @author lwhite
36  */

37 public class PersistenceStrategyBuilderFactory {
38     
39     /**
40     * The default path to the EE persistence strategy builders
41     *only used if getEEBuilderPath fails (which should only occur
42     *in unit-test
43     */

44     protected final String JavaDoc DEFAULT_EE_BUILDER_PATH = "com.sun.ejb.ee.sfsb.initialization";
45     
46     /** Creates a new instance of PersistenceStrategyBuilderFactory */
47     public PersistenceStrategyBuilderFactory() {
48         if (_logger == null) {
49             _logger = LogDomains.getLogger(LogDomains.EJB_LOGGER);
50         }
51     }
52     
53
54     /**
55      * creates the correct implementation of PersistenceStrategyBuilder
56      * if an invalid combination is input; an error is logged
57      * and MemoryStrategyBuilder is returned
58      *
59      * @param persistenceType
60      * @param container
61      * @param descriptor
62      */

63     public PersistenceStrategyBuilder createPersistenceStrategyBuilder(
64         String JavaDoc persistenceType)
65     {
66         PersistenceStrategyBuilder builder = new FileStrategyBuilder();
67         String JavaDoc className = createClassNameFrom(persistenceType);
68         _logger.finest("PersistenceStrategyBuilderFactory>>createPersistenceStrategyBuilder: "
69             + "CandidateBuilderClassName = " + className);
70     boolean requestedBuilderCreated = false;
71         try {
72             builder =
73                 (PersistenceStrategyBuilder) (Class.forName(className)).newInstance();
74         requestedBuilderCreated = true;
75         } catch (ClassNotFoundException JavaDoc clnfEx) {
76         _logger.log(Level.WARNING, "Couldn't find class: "
77             + className + " for persistenceType: "
78             + persistenceType);
79             _logger.log(Level.FINE, "Exception while creating Builder", clnfEx);
80         } catch (Exception JavaDoc ex) {
81             _logger.log(Level.WARNING, "Exception while creating Builder", ex);
82         }
83     if (! requestedBuilderCreated) {
84             _logger.log(Level.WARNING, "Couldn't create builder for "
85             + "persistence type: " + persistenceType
86             +". Instead created FileStrategyBuilder....");
87     }
88         return builder;
89     }
90     
91     /**
92      * returns an appropriately camel-cased String
93      * that is a candidate class name for a builder
94      * if persistenceType is "file" this returns
95      * the correct class name and package name for these classes
96      * i.e. com.sun.ejb.spi.??
97      * FIXME:figure this out where the file builder
98      * will reside
99      * otherwise they are in com.sun.appserv.ee.web.initialization
100      *
101      * @param persistenceType
102      */

103     private String JavaDoc createClassNameFrom(String JavaDoc persistenceType) {
104         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
105         //using package name will mean this will work
106
//even if class is moved to another package
107
String JavaDoc pkg = this.getClass().getPackage().getName();
108         if( !(persistenceType.equalsIgnoreCase("file")) )
109         {
110             //pkg is the package where EE builders MUST reside
111
//this defaults to
112
//"com.sun.enterprise.ee.web.initialization"
113
// but is configurable via (not-well-publicized)
114
//property in server.xml
115
//at "/server/availability-service/persistence-store/property[@name='ee-builder-path']"
116
pkg = this.getEEBuilderPath();
117         }
118         sb.append(pkg + ".");
119         sb.append(camelCase(persistenceType));
120         sb.append("StrategyBuilder");
121         String JavaDoc classname = sb.toString();
122         return classname;
123     }
124     
125     /**
126      * this method strips out all non-alpha characters; camelCases the result
127      *
128      * @param inputString
129      */

130     private String JavaDoc camelCase(String JavaDoc inputString) {
131         String JavaDoc strippedString = stripNonAlphas(inputString);
132         String JavaDoc firstLetter = (strippedString.substring(0, 1)).toUpperCase();
133         String JavaDoc remainingPart =
134             (strippedString.substring(1, strippedString.length())).toLowerCase();
135         return firstLetter + remainingPart;
136     }
137
138     /**
139      * this method strips out all non-alpha characters
140      *
141      * @param inputString
142      */

143     private String JavaDoc stripNonAlphas(String JavaDoc inputString) {
144         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(50);
145         for(int i=0; i<inputString.length(); i++) {
146             char nextChar = inputString.charAt(i);
147             if(Character.isLetter(nextChar)) {
148                 sb.append(nextChar);
149             }
150         }
151         return sb.toString();
152     }
153
154     /**
155      * return the path where the ee builders reside
156      * although this method allows this to be configurable
157      * via an property in server.xml we do not expose it
158      * and it should not be re-configured
159      *
160      */

161     private String JavaDoc getEEBuilderPath() {
162         return DEFAULT_EE_BUILDER_PATH;
163     }
164     
165     /**
166      * The logger to use for logging ALL ejb container related messages.
167      */

168     private static Logger JavaDoc _logger = null;
169     
170 }
171
Popular Tags