KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > sun > share > configbean > SessionConfigSubBean


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package org.netbeans.modules.j2ee.sun.share.configbean;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collection JavaDoc;
25
26 import javax.enterprise.deploy.spi.DConfigBean JavaDoc;
27 import javax.enterprise.deploy.spi.DeploymentConfiguration JavaDoc;
28 import javax.enterprise.deploy.spi.exceptions.ConfigurationException JavaDoc;
29 import javax.enterprise.deploy.model.DDBean JavaDoc;
30
31 import org.netbeans.modules.j2ee.sun.dd.api.CommonDDBean;
32 import org.netbeans.modules.j2ee.sun.dd.api.web.SunWebApp;
33 import org.netbeans.modules.j2ee.sun.dd.api.web.SessionConfig;
34 import org.netbeans.modules.j2ee.sun.dd.api.web.SessionManager;
35 import org.netbeans.modules.j2ee.sun.dd.api.web.ManagerProperties;
36 import org.netbeans.modules.j2ee.sun.dd.api.web.StoreProperties;
37 import org.netbeans.modules.j2ee.sun.dd.api.web.SessionProperties;
38 import org.netbeans.modules.j2ee.sun.dd.api.web.CookieProperties;
39
40
41 /** Property structure of session-config from DTD
42  *
43  * session-config : SessionConfig?
44  * session-manager : SessionManager?
45  * [attr: persistence-type CDATA memory]
46  * manager-properties : ManagerProperties?
47  * property : WebProperty[0,n]
48  * [attr: name CDATA #REQUIRED ]
49  * [attr: value CDATA #REQUIRED ]
50  * description : String?
51  * store-properties : StoreProperties?
52  * property : WebProperty[0,n]
53  * [attr: name CDATA #REQUIRED ]
54  * [attr: value CDATA #REQUIRED ]
55  * description : String?
56  * session-properties : SessionProperties?
57  * property : WebProperty[0,n]
58  * [attr: name CDATA #REQUIRED ]
59  * [attr: value CDATA #REQUIRED ]
60  * description : String?
61  * cookie-properties : CookieProperties?
62  * property : WebProperty[0,n]
63  * [attr: name CDATA #REQUIRED ]
64  * [attr: value CDATA #REQUIRED ]
65  * description : String?
66  */

67 /**
68  *
69  * @author Peter Williams
70  * @version %I%, %G%
71  */

72 public class SessionConfigSubBean {
73     
74     private static final String JavaDoc SunWebFileName = "sun-web.xml"; // NOI18N
75

76     private static final String JavaDoc SESSION_CONFIG = "session-config"; // NOI18N
77
// private static final String DEFAULT_PERSISTENCE_TYPE = "memory"; // NOI18N
78

79     /** takes place of Base.parent, which we don't have */
80     private WebAppRoot webAppRoot;
81     
82     /** Holds value of property persistenceType. */
83     private String JavaDoc persistenceType;
84     
85     /** Holds value of property managerProperties. */
86     private ManagerProperties managerProperties;
87     
88     /** Holds value of property storeProperties. */
89     private StoreProperties storeProperties;
90     
91     /** Holds value of property sessionProperties. */
92     private SessionProperties sessionProperties;
93     
94     /** Holds value of property cookieProperties. */
95     private CookieProperties cookieProperties;
96     
97     /** Creates a new instance of SessionConfiguration */
98     public SessionConfigSubBean() {
99     }
100     
101     /** Override init to load from persistent storage if necessary
102      * @param dDBean DDBean matching this bean
103      * @param parent Parent DConfigBean in the tree
104      */

105     protected void init(WebAppRoot parent) {
106         webAppRoot = parent;
107
108 // loadFromPlanFile(getConfig()); // !PW handled by WebAppRoot
109
}
110     
111     public Base getParent() {
112         return webAppRoot;
113     }
114     
115     /** Getter for property persistenceType.
116      * @return Value of property persistenceType.
117      *
118      */

119     public String JavaDoc getPersistenceType() {
120         return this.persistenceType;
121     }
122     
123     /** Setter for property persistenceType.
124      * @param newPersistenceType New value of property persistenceType.
125      *
126      * @throws PropertyVetoException
127      *
128      */

129     public void setPersistenceType(String JavaDoc newPersistenceType) throws java.beans.PropertyVetoException JavaDoc {
130         if(newPersistenceType == null || newPersistenceType.length() == 0) { // if empty, set to null
131
newPersistenceType = null;
132         }
133         
134         String JavaDoc oldPersistenceType = this.persistenceType;
135         getVCS().fireVetoableChange("persistenceType", oldPersistenceType, newPersistenceType); // NOI18N
136
this.persistenceType = newPersistenceType;
137         getPCS().firePropertyChange("persistenceType", oldPersistenceType, persistenceType); // NOI18N
138
}
139
140     /** Getter for property managerProperties.
141      * @return Value of property managerProperties.
142      *
143      */

144     public ManagerProperties getManagerProperties() {
145         return this.managerProperties;
146     }
147     
148     /** Setter for property managerProperties.
149      * @param managerProperties New value of property managerProperties.
150      *
151      * @throws PropertyVetoException
152      *
153      */

154     public void setManagerProperties(ManagerProperties newManagerProperties) throws java.beans.PropertyVetoException JavaDoc {
155         if(newManagerProperties == null) {
156             newManagerProperties = webAppRoot.getConfig().getStorageFactory().createManagerProperties();
157         }
158         
159         ManagerProperties oldManagerProperties = this.managerProperties;
160         getVCS().fireVetoableChange("managerProperties", oldManagerProperties, newManagerProperties); // NOI18N
161
this.managerProperties = newManagerProperties;
162         getPCS().firePropertyChange("managerProperties", oldManagerProperties, managerProperties); // NOI18N
163
}
164     
165     /** Getter for property storeProperties.
166      * @return Value of property storeProperties.
167      *
168      */

169     public StoreProperties getStoreProperties() {
170         return this.storeProperties;
171     }
172     
173     /** Setter for property storeProperties.
174      * @param storeProperties New value of property storeProperties.
175      *
176      * @throws PropertyVetoException
177      *
178      */

179     public void setStoreProperties(StoreProperties newStoreProperties) throws java.beans.PropertyVetoException JavaDoc {
180         if(newStoreProperties == null) {
181             newStoreProperties = webAppRoot.getConfig().getStorageFactory().createStoreProperties();
182         }
183         
184         StoreProperties oldStoreProperties = this.storeProperties;
185         getVCS().fireVetoableChange("storeProperties", oldStoreProperties, newStoreProperties); // NOI18N
186
this.storeProperties = newStoreProperties;
187         getPCS().firePropertyChange("storeProperties", oldStoreProperties, storeProperties); // NOI18N
188
}
189     
190     /** Getter for property sessionProperties.
191      * @return Value of property sessionProperties.
192      *
193      */

194     public SessionProperties getSessionProperties() {
195         return this.sessionProperties;
196     }
197     
198     /** Setter for property sessionProperties.
199      * @param sessionProperties New value of property sessionProperties.
200      *
201      * @throws PropertyVetoException
202      *
203      */

204     public void setSessionProperties(SessionProperties newSessionProperties) throws java.beans.PropertyVetoException JavaDoc {
205         if(newSessionProperties == null) {
206             newSessionProperties = webAppRoot.getConfig().getStorageFactory().createSessionProperties();
207         }
208         
209         SessionProperties oldSessionProperties = this.sessionProperties;
210         getVCS().fireVetoableChange("sessionProperties", oldSessionProperties, newSessionProperties); // NOI18N
211
this.sessionProperties = newSessionProperties;
212         getPCS().firePropertyChange("sessionProperties", oldSessionProperties, sessionProperties); // NOI18N
213
}
214     
215     /** Getter for property cookieProperties.
216      * @return Value of property cookieProperties.
217      *
218      */

219     public CookieProperties getCookieProperties() {
220         return this.cookieProperties;
221     }
222     
223     /** Setter for property cookieProperties.
224      * @param cookieProperties New value of property cookieProperties.
225      *
226      * @throws PropertyVetoException
227      *
228      */

229     public void setCookieProperties(CookieProperties newCookieProperties) throws java.beans.PropertyVetoException JavaDoc {
230         if(newCookieProperties == null) {
231             newCookieProperties = webAppRoot.getConfig().getStorageFactory().createCookieProperties();
232         }
233         
234         CookieProperties oldCookieProperties = this.cookieProperties;
235         getVCS().fireVetoableChange("cookieProperties", oldCookieProperties, newCookieProperties); // NOI18N
236
this.cookieProperties = newCookieProperties;
237         getPCS().firePropertyChange("cookieProperties", oldCookieProperties, cookieProperties); // NOI18N
238
}
239     
240     /* ------------------------------------------------------------------------
241      * Persistence support. Loads DConfigBeans from previously saved Deployment
242      * plan file.
243      */

244     Collection JavaDoc getSnippets() {
245         Collection JavaDoc snippets = new ArrayList JavaDoc();
246         Snippet snipOne = new Snippet() {
247             
248             public org.netbeans.modules.schema2beans.BaseBean getCmpDDSnippet() {
249                 return null;
250             }
251
252             public CommonDDBean getDDSnippet() {
253                 SessionConfig sessionConfig = webAppRoot.getConfig().getStorageFactory().createSessionConfig();
254                 String JavaDoc version = webAppRoot.getAppServerVersion().getWebAppVersionAsString();
255                 
256                 SessionManager sessionManager = sessionConfig.newSessionManager();
257                 boolean hasSessionManager = false;
258                 
259                 // Set each setting only if present and set by the user.
260
String JavaDoc persistenceType = getPersistenceType();
261                 if(Utils.notEmpty(persistenceType)) {
262                     sessionManager.setPersistenceType(getPersistenceType());
263                     hasSessionManager = true;
264                 } else {
265                     sessionManager.setPersistenceType(null);
266                 }
267
268                 ManagerProperties mp = getManagerProperties();
269                 if(mp.sizeWebProperty() > 0) {
270                     sessionManager.setManagerProperties((ManagerProperties) mp.cloneVersion(version));
271                     hasSessionManager = true;
272                 }
273
274                 StoreProperties sp = getStoreProperties();
275                 if(sp.sizeWebProperty() > 0) {
276                     sessionManager.setStoreProperties((StoreProperties) sp.cloneVersion(version));
277                     hasSessionManager = true;
278                 }
279
280                 if(hasSessionManager) {
281                     sessionConfig.setSessionManager(sessionManager);
282                 }
283
284                 SessionProperties ssp = getSessionProperties();
285                 if(ssp.sizeWebProperty() > 0) {
286                     sessionConfig.setSessionProperties((SessionProperties) ssp.cloneVersion(version));
287                 }
288
289                 CookieProperties cp = getCookieProperties();
290                 if(cp.sizeWebProperty() > 0) {
291                     sessionConfig.setCookieProperties((CookieProperties) cp.cloneVersion(version));
292                 }
293                 
294                 return sessionConfig;
295             }
296
297             public boolean hasDDSnippet() {
298                 if(getPersistenceType() != null) {
299                     return true;
300                 }
301                 
302                 ManagerProperties mp = getManagerProperties();
303                 if(mp.sizeWebProperty() > 0) {
304                     return true;
305                 }
306                 
307                 StoreProperties sp = getStoreProperties();
308                 if(sp.sizeWebProperty() > 0) {
309                     return true;
310                 }
311                 
312                 SessionProperties ssp = getSessionProperties();
313                 if(ssp.sizeWebProperty() > 0) {
314                     return true;
315                 }
316                 
317                 CookieProperties cp = getCookieProperties();
318                 if(cp.sizeWebProperty() > 0) {
319                     return true;
320                 }
321                 
322                 return false;
323             }
324             
325             public String JavaDoc getPropertyName() {
326                 return SunWebApp.SESSION_CONFIG;
327             }
328             
329 /** ---------------------------------------------------------------------------
330  * The following methods would have been inherited from DefaultSnippet if we
331  * could derive from that class. (Another artifact of not being a real DConfigBean
332  */

333             public String JavaDoc getFileName() {
334                 return SunWebFileName; // NOI18N
335
}
336             
337             public CommonDDBean mergeIntoRootDD(CommonDDBean ddRoot) {
338                 SessionConfig sessionConfig = (SessionConfig) getDDSnippet();
339                 
340                 if(ddRoot instanceof SunWebApp) {
341                     SunWebApp swa = (SunWebApp) ddRoot;
342                     swa.setSessionConfig(sessionConfig);
343                 }
344                 
345                 return sessionConfig;
346             }
347
348             public CommonDDBean mergeIntoRovingDD(CommonDDBean ddParent) {
349                 // !PW I don't think this can ever be called, but if so, it should
350
// be called with ddParent being an instance of SunWebApp and so
351
// mergeIntoRootDD() performs the correct action.
352
//
353
return mergeIntoRootDD(ddParent);
354             }
355 /** ------------------------------------------------------------------------- */
356             
357         };
358         
359         snippets.add(snipOne);
360         return snippets;
361     }
362         
363     private class SessionConfigFinder implements ConfigFinder {
364         public Object JavaDoc find(Object JavaDoc obj) {
365             SessionConfig result = null;
366             
367             if(obj instanceof SunWebApp) {
368                 SunWebApp swa = (SunWebApp) obj;
369                 
370                 result = swa.getSessionConfig();
371             }
372             
373             return result;
374         }
375     }
376     
377     boolean loadFromPlanFile(SunONEDeploymentConfiguration config) {
378         String JavaDoc uriText = webAppRoot.getUriText();
379         
380         SessionConfig beanGraph = (SessionConfig) config.getBeans(
381             uriText, SunWebFileName, webAppRoot.getParser(), new SessionConfigFinder());
382         
383         clearProperties();
384         
385         if(null != beanGraph) {
386             SessionManager sm = beanGraph.getSessionManager();
387             if(sm != null) {
388                 persistenceType = sm.getPersistenceType();
389                 
390                 ManagerProperties mp = sm.getManagerProperties();
391                 if(mp != null && mp.sizeWebProperty() > 0) {
392                     managerProperties = (ManagerProperties) mp.clone();
393                 }
394
395                 StoreProperties sp = sm.getStoreProperties();
396                 if(sp != null && sp.sizeWebProperty() > 0) {
397                     storeProperties = (StoreProperties) sp.clone();
398                 }
399             }
400             
401             SessionProperties ssp = beanGraph.getSessionProperties();
402             if(ssp != null && ssp.sizeWebProperty() > 0) {
403                 sessionProperties = (SessionProperties) ssp.clone();
404             }
405
406             CookieProperties cp = beanGraph.getCookieProperties();
407             if(cp != null && cp.sizeWebProperty() > 0) {
408                 cookieProperties = (CookieProperties) cp.clone();
409             }
410         } else {
411             setDefaultProperties();
412         }
413         
414         return (beanGraph != null);
415     }
416     
417     protected void clearProperties() {
418         StorageBeanFactory beanFactory = webAppRoot.getConfig().getStorageFactory();
419         
420         persistenceType = null;
421         managerProperties = beanFactory.createManagerProperties();
422         storeProperties = beanFactory.createStoreProperties();
423         sessionProperties = beanFactory.createSessionProperties();
424         cookieProperties = beanFactory.createCookieProperties();
425     }
426     
427     protected void setDefaultProperties() {
428         // no defaults
429
}
430     
431     /** -----------------------------------------------------------------------
432      * JavaBean support that would have been inherited from Base if this was a
433      * true DConfigBean.
434      *
435      * !PW I amended this code to forward property notifications
436      * to the webAppRoot parent DConfigBean.
437      */

438     /**
439      * @return PropertyChangeSupport object used by this bean.
440      */

441     protected java.beans.PropertyChangeSupport JavaDoc getPCS() {
442         return webAppRoot.getPCS();
443     }
444
445     /**
446      * @return VetoableChangeSupport object used by this bean.
447      */

448     protected java.beans.VetoableChangeSupport JavaDoc getVCS() {
449         return webAppRoot.getVCS();
450     }
451 }
452
Popular Tags