KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jca > ResourceDeploy


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jca;
30
31 import com.caucho.config.ConfigException;
32 import com.caucho.lifecycle.Lifecycle;
33 import com.caucho.server.deploy.DeployController;
34 import com.caucho.server.util.CauchoSystem;
35 import com.caucho.util.L10N;
36 import com.caucho.vfs.Path;
37 import com.caucho.vfs.Vfs;
38
39 import javax.annotation.PostConstruct;
40 import java.io.IOException JavaDoc;
41 import java.util.ArrayList JavaDoc;
42 import java.util.HashSet JavaDoc;
43 import java.util.Iterator JavaDoc;
44
45 /**
46  * The generator for the resource-deploy
47  */

48 public class ResourceDeploy {
49   private static final L10N L = new L10N(ResourceDeploy.class);
50
51   private final ResourceDeployAdmin _admin = new ResourceDeployAdmin(this);
52
53   private Path _containerRootDirectory;
54
55   private Path _rarDir;
56   private Path _rarExpandDir;
57
58   private String JavaDoc _expandPrefix = "";
59
60   private HashSet JavaDoc<String JavaDoc> _rarNames = new HashSet JavaDoc<String JavaDoc>();
61
62   private volatile boolean _isInit;
63
64   public ResourceDeploy()
65   {
66     setExpandPrefix("_rar_");
67
68     _containerRootDirectory = Vfs.getPwd();
69
70   }
71
72   Path getContainerRootDirectory()
73   {
74     return _containerRootDirectory;
75   }
76
77   /**
78    * Gets the rar directory.
79    */

80   public Path getPath()
81   {
82     return _rarDir;
83   }
84
85   /**
86    * Sets the rar directory.
87    */

88   public void setPath(Path path)
89   {
90     _rarDir = path;
91   }
92
93   public Path getArchiveDirectory()
94   {
95     return getPath();
96   }
97
98   public Path getArchivePath(String JavaDoc name)
99   {
100     return getArchiveDirectory().lookup(name + getExtension());
101   }
102
103   /**
104    * Returns the combination of prefix, name, and suffix used for expanded
105    * archives.
106    *
107    * @return
108    */

109   protected String JavaDoc getExpandName(String JavaDoc name)
110   {
111     return getExpandPrefix() + name + getExpandSuffix();
112   }
113
114
115   /**
116    * Sets the war expand dir to check for new applications.
117    */

118   public void setExpandPath(Path path)
119   {
120     _rarExpandDir = path;
121   }
122
123   /**
124    * @deprecated use {@link @getExpandDirectory}
125    */

126   public Path getExpandPath()
127   {
128     return getExpandDirectory();
129   }
130
131   /**
132    * Gets the rar expand directory.
133    */

134   public Path getExpandDirectory()
135   {
136     if (_rarExpandDir != null)
137       return _rarExpandDir;
138     else
139       return _rarDir;
140   }
141
142   /**
143    * Returns the location of an expanded archive, or null if no archive with
144    * the passed name is deployed.
145    *
146    * @param name a name, without an extension
147    */

148   public Path getExpandPath(String JavaDoc name)
149   {
150     if (!isDeployedKey(name))
151       return null;
152
153     return getExpandDirectory().lookup(getExpandName(name));
154   }
155
156   private boolean isDeployedKey(String JavaDoc name)
157   {
158     return _rarNames.contains(name);
159   }
160
161   /**
162    * Returns the expand prefix.
163    */

164   public String JavaDoc getExpandPrefix()
165   {
166     return _expandPrefix;
167   }
168
169   /**
170    * Sets the expand prefix.
171    */

172   public void setExpandPrefix(String JavaDoc prefix)
173   {
174     _expandPrefix = prefix;
175   }
176
177   public boolean isModified()
178   {
179     try {
180       return ! _rarNames.equals(getRarNames());
181     } catch (Exception JavaDoc e) {
182       return false;
183     }
184   }
185
186   /**
187    * Initialize the resource-deploy.
188    */

189   @PostConstruct
190   public void init()
191     throws ConfigException
192   {
193     synchronized (this) {
194       if (_isInit)
195     return;
196       _isInit = true;
197     }
198     
199     if (getPath() == null)
200       throw new ConfigException(L.l("resource-deploy requires a path attribute"));
201
202     try {
203       _rarNames = getRarNames();
204       
205       Iterator JavaDoc<String JavaDoc> iter = _rarNames.iterator();
206       while (iter.hasNext()) {
207     String JavaDoc name = iter.next();
208
209     ResourceArchive rar;
210
211     rar = new ResourceArchive();
212     rar.setRarPath(getPath().lookup(name + ".rar"));
213     rar.setRootDirectory(getExpandPath().lookup(getExpandPrefix() + name));
214
215     Path oldPwd = Vfs.getPwd();
216
217     try {
218       Vfs.setPwd(rar.getRootDirectory());
219
220       for (ResourceConfig config : ResourceDefault.getDefaultList()) {
221         config.getBuilderProgram().configure(rar);
222       }
223     } finally {
224       Vfs.setPwd(oldPwd);
225     }
226
227     rar.init();
228
229     ResourceArchiveManager.addResourceArchive(rar);
230       }
231     } catch (ConfigException e) {
232       throw e;
233     } catch (Throwable JavaDoc e) {
234       throw new ConfigException(e);
235     }
236
237     _admin.register();
238   }
239
240   /**
241    * Return the war-expansion directories which were added.
242    */

243   private HashSet JavaDoc<String JavaDoc> getRarNames()
244     throws IOException JavaDoc
245   {
246     HashSet JavaDoc<String JavaDoc> rarNames = new HashSet JavaDoc<String JavaDoc>();
247
248     Path rarDir = getPath();
249     Path rarExpandDir = getExpandPath();
250
251     if (rarDir == null || rarExpandDir == null)
252       return rarNames;
253
254     String JavaDoc []rarDirList = rarDir.list();
255
256     // collect all the new wars
257
loop:
258     for (int i = 0; i < rarDirList.length; i++) {
259       String JavaDoc rarName = rarDirList[i];
260       String JavaDoc appName;
261
262       if (! rarName.endsWith(".rar"))
263         continue;
264
265       Path path = rarDir.lookup(rarName);
266
267       if (! path.canRead())
268         continue;
269
270       appName = rarName.substring(0, rarName.length() - 4);
271
272       if (CauchoSystem.isCaseInsensitive())
273         appName = appName.toLowerCase();
274
275       rarNames.add(appName);
276     }
277     
278     String JavaDoc []rarExpandList = rarExpandDir.list();
279     ArrayList JavaDoc<String JavaDoc> newNames = new ArrayList JavaDoc<String JavaDoc>();
280
281     // collect all the new rar expand directories
282
loop:
283     for (int i = 0; i < rarExpandList.length; i++) {
284       String JavaDoc rarDirName = rarExpandList[i];
285
286       if (! rarDirName.startsWith(getExpandPrefix()))
287     continue;
288
289       if (CauchoSystem.isCaseInsensitive())
290         rarDirName = rarDirName.toLowerCase();
291
292       Path path = rarExpandDir.lookup(rarDirName);
293
294       if (! path.isDirectory() || ! rarDirName.startsWith(getExpandPrefix()))
295         continue;
296       
297       String JavaDoc appName = rarDirName.substring(getExpandPrefix().length());
298
299       if (! newNames.contains(appName))
300         newNames.add(appName);
301
302       rarNames.add(appName);
303     }
304
305     return rarNames;
306   }
307
308   public String JavaDoc getExtension()
309   {
310     return ".rar";
311   }
312
313   public String JavaDoc getExpandSuffix()
314   {
315     return "";
316   }
317
318   public long getDependencyCheckInterval()
319   {
320     return -1;
321   }
322
323   public String JavaDoc getStartupMode()
324   {
325     return DeployController.STARTUP_AUTOMATIC;
326   }
327
328   public String JavaDoc getRedeployMode()
329   {
330     return DeployController.REDEPLOY_MANUAL;
331   }
332
333   public String JavaDoc getState()
334   {
335     if (!_isInit)
336       return Lifecycle.getStateName(Lifecycle.IS_NEW);
337     else
338       return Lifecycle.getStateName(Lifecycle.IS_ACTIVE);
339   }
340
341   public void start()
342   {
343     // XXX:
344
}
345
346   public Throwable JavaDoc getConfigException()
347   {
348     // XXX:
349
return null;
350   }
351
352   public void stop()
353   {
354     // XXX:
355
}
356
357   public void update()
358   {
359     // XXX:
360
}
361
362   public String JavaDoc[] getNames()
363   {
364     // XXX:
365
return new String JavaDoc[0];
366   }
367
368   public void stop(String JavaDoc name)
369   {
370     // XXX:
371
}
372
373   public void start(String JavaDoc name)
374   {
375     // XXX:
376
}
377
378   public Throwable JavaDoc getConfigException(String JavaDoc moduleID)
379   {
380     // XXX:
381
return null;
382   }
383
384   public void undeploy(String JavaDoc name)
385   {
386     // XXX:
387
}
388
389 }
390
Popular Tags