KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > webapp > WebAppRegexpDeployGenerator


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.server.webapp;
30
31 import com.caucho.config.types.PathBuilder;
32 import com.caucho.log.Log;
33 import com.caucho.server.deploy.DeployContainer;
34 import com.caucho.server.deploy.DeployGenerator;
35 import com.caucho.vfs.Path;
36
37 import java.util.ArrayList JavaDoc;
38 import java.util.HashMap JavaDoc;
39 import java.util.logging.Level JavaDoc;
40 import java.util.logging.Logger JavaDoc;
41 import java.util.regex.Matcher JavaDoc;
42 import java.util.regex.Pattern JavaDoc;
43
44 /**
45  * The generator for the web-app deploy
46  */

47 public class WebAppRegexpDeployGenerator
48   extends DeployGenerator<WebAppController> {
49   private static final Logger JavaDoc log = Log.open(WebAppSingleDeployGenerator.class);
50
51   private WebAppContainer _container;
52   
53   private WebAppController _parent;
54
55   private WebAppConfig _config;
56   
57   private ArrayList JavaDoc<WebAppConfig> _webAppDefaults =
58     new ArrayList JavaDoc<WebAppConfig>();
59
60   private ArrayList JavaDoc<WebAppController> _entries =
61     new ArrayList JavaDoc<WebAppController>();
62
63   /**
64    * Creates the new host deploy.
65    */

66   public WebAppRegexpDeployGenerator(DeployContainer<WebAppController> deployContainer)
67   {
68     super(deployContainer);
69   }
70
71   /**
72    * Creates the new host deploy.
73    */

74   public WebAppRegexpDeployGenerator(DeployContainer<WebAppController> deployContainer,
75                 WebAppContainer container,
76                 WebAppConfig config)
77   {
78     super(deployContainer);
79     
80     setContainer(container);
81
82     _config = config;
83   }
84
85   /**
86    * Gets the webApp container.
87    */

88   public WebAppContainer getContainer()
89   {
90     return _container;
91   }
92
93   /**
94    * Sets the webApp container.
95    */

96   public void setContainer(WebAppContainer container)
97   {
98     _container = container;
99   }
100   /**
101    * Sets the parent webApp.
102    */

103   public void setParent(WebAppController parent)
104   {
105     _parent = parent;
106   }
107   
108   /**
109    * Returns the current array of webApp entries.
110    */

111   public WebAppController generateController(String JavaDoc name)
112   {
113     Pattern JavaDoc regexp = _config.getURLRegexp();
114     Matcher JavaDoc matcher = regexp.matcher(name);
115
116     if (! matcher.find() || matcher.start() != 0)
117       return null;
118
119     int length = matcher.end() - matcher.start();
120
121     String JavaDoc contextPath = matcher.group();
122         
123     ArrayList JavaDoc<String JavaDoc> vars = new ArrayList JavaDoc<String JavaDoc>();
124
125     //WebAppDeployControlleryController entry = new WebAppDeployControlleryController(this, contextPath);
126
HashMap JavaDoc<String JavaDoc,Object JavaDoc> varMap = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
127     // entry.getVariableMap();
128

129     for (int j = 0; j <= matcher.groupCount(); j++) {
130       vars.add(matcher.group(j));
131       varMap.put("app" + j, matcher.group(j));
132     }
133
134     varMap.put("regexp", vars);
135
136     Path appDir = null;
137     
138     try {
139       String JavaDoc appDirPath = _config.getDocumentDirectory();
140
141       if (appDirPath == null)
142     appDirPath = "./" + matcher.group(0);
143       
144       appDir = PathBuilder.lookupPath(appDirPath, varMap);
145       
146       if (! appDir.canRead())
147     return null;
148     } catch (Exception JavaDoc e) {
149       log.log(Level.FINER, e.toString(), e);
150       
151       return null;
152     }
153
154     WebAppController controller = null;
155     
156     Thread JavaDoc thread = Thread.currentThread();
157     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
158
159     try {
160       thread.setContextClassLoader(getParentClassLoader());
161       
162       synchronized (_entries) {
163     for (int i = 0; i < _entries.size(); i++) {
164       controller = _entries.get(i);
165
166       if (appDir.equals(controller.getRootDirectory()))
167         return controller;
168     }
169
170     controller = new WebAppController(name, appDir, _container);
171
172     // XXX: not dynamic-deploy in the sense that the mappings are known
173
//controller.setDynamicDeploy(true);
174
controller.getVariableMap().putAll(varMap);
175     controller.setRegexpValues(vars);
176     controller.setConfig(_config);
177     // _controller.setJarPath(_archivePath);
178

179     for (int i = 0; i < _webAppDefaults.size(); i++)
180       controller.addConfigDefault(_webAppDefaults.get(i));
181       
182     _entries.add(controller);
183       }
184     } finally {
185       thread.setContextClassLoader(oldLoader);
186     }
187
188     controller.setSourceType("regexp");
189     
190     //controller.deploy();
191

192     return controller;
193   }
194
195   public String JavaDoc toString()
196   {
197     if (_config == null)
198       return "WebAppRegexpDeployGenerator[]";
199     else
200       return "WebAppRegexpDeployGenerator[" + _config.getURLRegexp().pattern() + "]";
201   }
202 }
203
Popular Tags