KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > admin > wsmgmt > repository > impl > cache > CacheMgr


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 package com.sun.enterprise.admin.wsmgmt.repository.impl.cache;
24
25 import java.util.List JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Properties JavaDoc;
29 import java.io.FileInputStream JavaDoc;
30 import java.io.FileOutputStream JavaDoc;
31 import java.io.File JavaDoc;
32 import java.util.Collection JavaDoc;
33 import java.util.Iterator JavaDoc;
34 import java.util.Enumeration JavaDoc;
35 import com.sun.enterprise.util.SystemPropertyConstants;
36 import com.sun.enterprise.admin.servermgmt.pe.PEFileLayout;
37
38 import java.util.logging.Logger JavaDoc;
39 import java.util.logging.Level JavaDoc;
40 import com.sun.logging.LogDomains;
41
42 /**
43  * Responsible for keeping track of the deployed applications and stand
44  * alone ejb and web modules with web services. This is a singleton for
45  * the domain. This class is intended to run on the Administration Server.
46  *
47  * @author Nazrul Islam
48  * @since J2SE 5.0
49  */

50 public class CacheMgr {
51
52     /**
53      * Returns the singleton instance of this class.
54      *
55      * @return sigleton instance of this class
56      */

57     public static CacheMgr getInstance() {
58         if (_mgr == null) {
59             _mgr = new CacheMgr();
60         }
61
62         return _mgr;
63     }
64
65     /**
66      * Private constructor. Loads the cache index from the persistent store.
67      */

68     private CacheMgr() {
69         load();
70     }
71
72     /**
73      * Returns the persistent cache index location.
74      *
75      * @return the persistent cache file location
76      */

77     private String JavaDoc getPropertyFile() {
78         String JavaDoc iRoot =
79             System.getProperty(SystemPropertyConstants.INSTANCE_ROOT_PROPERTY);
80         String JavaDoc file = iRoot + File.separator + PEFileLayout.GENERATED_DIR
81                     + File.separator + CACHE_FILE;
82         return file;
83     }
84
85    /**
86     * Saves the current in memory cache indexes to a property file.
87     */

88    void save() {
89
90         FileOutputStream JavaDoc fos = null;
91         try {
92             Properties JavaDoc pro = new Properties JavaDoc();
93
94             Collection JavaDoc eValues = _ejbModules.values();
95             for (Iterator JavaDoc iter=eValues.iterator(); iter.hasNext();) {
96                 String JavaDoc ejbMod = (String JavaDoc) iter.next();
97                 pro.put(ejbMod, EJB);
98             }
99             Collection JavaDoc wValues = _webModules.values();
100             for (Iterator JavaDoc iter=wValues.iterator(); iter.hasNext();) {
101                 String JavaDoc webMod = (String JavaDoc) iter.next();
102                 pro.put(webMod, WEB);
103             }
104             Collection JavaDoc aValues = _j2eeApplications.values();
105             for (Iterator JavaDoc iter=aValues.iterator(); iter.hasNext();) {
106                 J2eeApplication app = (J2eeApplication) iter.next();
107                 pro.put(app.getName(), app.getPersistentValue());
108             }
109
110             File JavaDoc file = new File JavaDoc(getPropertyFile());
111             fos = new FileOutputStream JavaDoc(file);
112             pro.store(fos, "");
113
114         } catch (Exception JavaDoc e) {
115             _logger.log(Level.FINE, "Error while saving ws-mgmt cache file", e);
116         } finally {
117             if (fos != null) {
118                 try {
119                     fos.close();
120                 } catch (Exception JavaDoc e) { }
121             }
122         }
123     }
124
125     /**
126      * Reads from the persistent cache index property file.
127      */

128     private void load() {
129
130         FileInputStream JavaDoc fis = null;
131         try {
132             File JavaDoc file = new File JavaDoc(getPropertyFile());
133
134             // abort if file does not exist
135
if (!file.exists()) {
136                 return;
137             }
138
139             fis = new FileInputStream JavaDoc(file);
140             Properties JavaDoc pro = new Properties JavaDoc();
141             pro.load(fis);
142
143             for (Enumeration JavaDoc e = pro.keys(); e.hasMoreElements();) {
144                 String JavaDoc key = (String JavaDoc) e.nextElement();
145                 String JavaDoc val = pro.getProperty(key);
146
147                 if (EJB.equals(val)) {
148                     _ejbModules.put(key, key);
149                 } else if (WEB.equals(val)) {
150                     _webModules.put(key, key);
151                 } else {
152                     J2eeApplication app = new J2eeApplication(key, val);
153                     _j2eeApplications.put(key, app);
154                 }
155             }
156         } catch (Exception JavaDoc e) {
157             _logger.log(Level.FINE,
158                 "Error while loading ws-mgmt cache file", e);
159         } finally {
160             if (fis != null) {
161                 try {
162                     fis.close();
163                 } catch (Exception JavaDoc e) { }
164             }
165         }
166     }
167
168     /**
169      * Adds an application to the cache.
170      *
171      * @param name name of the application
172      * @param ejbBundles ejb bundles with web services
173      * @param webBundles web bundles with web services
174      */

175     void addJ2eeApplication(String JavaDoc name, List JavaDoc ejbBundles, List JavaDoc webBundles) {
176
177         J2eeApplication app = new J2eeApplication(name, ejbBundles, webBundles);
178         _j2eeApplications.put(name, app);
179     }
180
181     /**
182      * Removes an application from the cache.
183      *
184      * @param name name of the application
185      */

186     J2eeApplication removeJ2eeApplication(String JavaDoc name) {
187         return (J2eeApplication) _j2eeApplications.remove(name);
188     }
189
190     /**
191      * Returns all j2ee applications with web services.
192      *
193      * @return j2ee applications with web services
194      */

195     public Map JavaDoc getJ2eeApplications() {
196         return _j2eeApplications;
197     }
198
199     /**
200      * Returns all stand alone ejb modules with web services.
201      *
202      * @return stand alone ejb module with web services
203      */

204     public Map JavaDoc getEjbModules() {
205         return _ejbModules;
206     }
207
208     /**
209      * Adds an ejb module to the cache.
210      *
211      * @param name name of the stand alone ejb module with web services
212      */

213     void addEjbModule(String JavaDoc name) {
214         _ejbModules.put(name, name);
215     }
216
217     /**
218      * Removes an ejb module from cache.
219      *
220      * @param name name of the stand alone ejb module
221      */

222     String JavaDoc removeEjbModule(String JavaDoc name) {
223         return (String JavaDoc) _ejbModules.remove(name);
224     }
225
226     /**
227      * Returns all stand alone web apps with web services.
228      *
229      * @return web apps with web services
230      */

231     public Map JavaDoc getWebModules() {
232         return _webModules;
233     }
234
235     /**
236      * Adds a web module to the cache.
237      *
238      * @param name name of the stand alone web module with web services
239      */

240     void addWebModule(String JavaDoc name) {
241         _webModules.put(name, name);
242     }
243
244     /**
245      * Removes a web module from cache.
246      *
247      * @param name name of the stand alone web module
248      */

249     String JavaDoc removeWebModule(String JavaDoc name) {
250         return (String JavaDoc) _webModules.remove(name);
251     }
252
253     // ---- VARIABLES - PRIVATE -----------------------------
254
private static CacheMgr _mgr = null;
255     private Map JavaDoc _j2eeApplications = new HashMap JavaDoc();
256     private Map JavaDoc _ejbModules = new HashMap JavaDoc();
257     private Map JavaDoc _webModules = new HashMap JavaDoc();
258     private static final String JavaDoc EJB = "EJB_MODULE";
259     private static final String JavaDoc WEB = "WEB_MODULE";
260     private static final String JavaDoc CACHE_FILE = ".com_sun_appserv_wsindex";
261
262     private static Logger JavaDoc _logger = Logger.getLogger(LogDomains.ADMIN_LOGGER);
263 }
264
Popular Tags