KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > web > 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 /*
25  * PersistenceStrategyBuilderFactory.java
26  *
27  * Created on September 30, 2002, 11:16 AM
28  */

29
30 package com.sun.enterprise.web;
31
32 import java.util.logging.Logger JavaDoc;
33 import java.util.logging.Level JavaDoc;
34 import com.sun.logging.LogDomains;
35 import org.apache.catalina.Context;
36
37
38 public class PersistenceStrategyBuilderFactory {
39     
40   /**
41   * The default path to the EE persistence strategy builders
42    *only used if getEEBuilderPath fails (which should only occur
43    *in unit-test
44   */

45   protected final String JavaDoc DEFAULT_EE_BUILDER_PATH = "com.sun.enterprise.ee.web.initialization";
46
47     /**
48      * return the path where the ee builders reside
49      * although this method allows this to be configurable
50      * via an property in server.xml we do not expose it
51      * and it should not be re-configured
52      *
53      */

54     private String JavaDoc getEEBuilderPath() {
55         if(_eeBuilderPath == null) {
56             ServerConfigLookup lookup = new ServerConfigLookup();
57             _eeBuilderPath = lookup.getEEBuilderPathFromConfig();
58         }
59         _logger.finest("_eeBuilderPath = " + _eeBuilderPath);
60         return _eeBuilderPath;
61     }
62     
63     /** Creates a new instance of PersistenceStrategyBuilderFactory */
64     public PersistenceStrategyBuilderFactory() {
65         if (_logger == null) {
66             _logger = LogDomains.getLogger(LogDomains.WEB_LOGGER);
67         }
68     }
69
70     /**
71      * creates the correct implementation of PersistenceStrategyBuilder
72      * if an invalid combination is input; an error is logged
73      * and MemoryStrategyBuilder is returned
74      *
75      * @param persistenceType
76      * @param frequency
77      * @param scope
78      * @param ctx
79      */

80     PersistenceStrategyBuilder createPersistenceStrategyBuilder(String JavaDoc persistenceType, String JavaDoc frequency, String JavaDoc scope, Context ctx) {
81
82         PersistenceStrategyBuilder builder = new MemoryStrategyBuilder();
83         String JavaDoc className = createClassNameFrom(persistenceType, frequency, scope);
84         _logger.finest("PersistenceStrategyBuilderFactory>>createPersistenceStrategyBuilder: "
85             + "CandidateBuilderClassName = " + className);
86         try {
87             builder =
88                 (PersistenceStrategyBuilder) (Class.forName(className)).newInstance();
89         } catch (Exception JavaDoc ex) {
90             Object JavaDoc[] params = { getApplicationId(ctx), persistenceType, frequency, scope };
91             _logger.log(Level.WARNING,
92                         "webcontainer.invalidSessionManagerConfig",
93                         params);
94         }
95         builder.setPersistenceFrequency(frequency);
96         builder.setPersistenceScope(scope);
97         return builder;
98     }
99
100     /**
101      * creates the correct implementation of PersistenceStrategyBuilder
102      * if an invalid combination is input; an error is logged
103      * and MemoryStrategyBuilder is returned
104      *
105      * @param persistenceType
106      * @param frequency
107      * @param scope
108      */

109     PersistenceStrategyBuilder createPersistenceStrategyBuilder(String JavaDoc persistenceType, String JavaDoc frequency, String JavaDoc scope) {
110         
111         PersistenceStrategyBuilder builder = new MemoryStrategyBuilder();
112         String JavaDoc className = createClassNameFrom(persistenceType, frequency, scope);
113         _logger.finest("PersistenceStrategyBuilderFactory>>createPersistenceStrategyBuilder: "
114             + "CandidateBuilderClassName = " + className);
115         try {
116             builder =
117                 (PersistenceStrategyBuilder) (Class.forName(className)).newInstance();
118         } catch (Exception JavaDoc ex) {
119             Object JavaDoc[] params = { persistenceType, frequency, scope };
120             _logger.log(Level.WARNING,
121                         "webcontainer.invalidSessionManagerConfig",
122                         params);
123         }
124         builder.setPersistenceFrequency(frequency);
125         builder.setPersistenceScope(scope);
126         return builder;
127     }
128
129     /**
130      * returns the application id for the module
131      *
132      * @param ctx the context
133      */

134     public String JavaDoc getApplicationId(Context ctx) {
135         com.sun.enterprise.web.WebModule wm =
136         (com.sun.enterprise.web.WebModule)ctx;
137         return wm.getID();
138     }
139     
140     //this method used strictly for testing
141
//do not use this method for any other purpose!
142
public PersistenceStrategyBuilder testCreatePersistenceStategyBuilder(String JavaDoc persistenceType, String JavaDoc frequency, String JavaDoc scope) {
143         return this.createPersistenceStrategyBuilder(persistenceType, frequency, scope);
144     }
145
146     /**
147      * returns an appropriately camel-cased String
148      * that is a candidate class name for a builder
149      * if persistenceType is "memory" or "file" this returns
150      * the correct class name and package name for these classes
151      * i.e. com.iplanet.ias.web
152      * otherwise they are in com.sun.appserv.ee.web.initialization
153      *
154      * @param persistenceType
155      * @param frequency
156      * @param scope
157      */

158     private String JavaDoc createClassNameFrom(String JavaDoc persistenceType, String JavaDoc frequency, String JavaDoc scope) {
159         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
160         //using package name will mean this will work
161
//even if class is moved to another package
162
String JavaDoc pkg = this.getClass().getPackage().getName();
163         if( !(persistenceType.equalsIgnoreCase("memory")
164             | persistenceType.equalsIgnoreCase("file")) )
165         {
166             //pkg is the package where EE builders MUST reside
167
//this defaults to
168
//"com.sun.enterprise.ee.web.initialization"
169
// but is configurable via (not-well-publicized)
170
//property in server.xml
171
//at "/server/availability-service/persistence-store/property[@name='ee-builder-path']"
172
pkg = this.getEEBuilderPath();
173         }
174         sb.append(pkg + ".");
175         sb.append(camelCase(persistenceType));
176         if(frequency != null) {
177             sb.append(camelCase(frequency));
178         }
179         if(scope != null) {
180             sb.append(camelCase(scope));
181         }
182         sb.append("StrategyBuilder");
183         String JavaDoc classname = sb.toString();
184         return classname;
185     }
186
187     /**
188      * this method strips out all non-alpha characters; camelCases the result
189      *
190      * @param inputString
191      */

192     private String JavaDoc camelCase(String JavaDoc inputString) {
193         String JavaDoc strippedString = stripNonAlphas(inputString);
194         String JavaDoc firstLetter = (strippedString.substring(0, 1)).toUpperCase();
195         String JavaDoc remainingPart =
196             (strippedString.substring(1, strippedString.length())).toLowerCase();
197         return firstLetter + remainingPart;
198     }
199
200     /**
201      * this method strips out all non-alpha characters
202      *
203      * @param inputString
204      */

205     private String JavaDoc stripNonAlphas(String JavaDoc inputString) {
206         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(50);
207         for(int i=0; i<inputString.length(); i++) {
208             char nextChar = inputString.charAt(i);
209             if(Character.isLetter(nextChar)) {
210                 sb.append(nextChar);
211             }
212         }
213         return sb.toString();
214     }
215     
216     /**
217      * The logger to use for logging ALL web container related messages.
218      */

219     private static Logger JavaDoc _logger = null;
220     
221     /**
222      * The path where ee builders reside
223      */

224     private String JavaDoc _eeBuilderPath = null;
225     
226 }
227
Popular Tags