KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > server > host > HostRegexpDeployGenerator


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  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.server.host;
31
32 import com.caucho.config.Config;
33 import com.caucho.log.Log;
34 import com.caucho.server.deploy.DeployContainer;
35 import com.caucho.server.deploy.DeployGenerator;
36 import com.caucho.vfs.Path;
37
38 import java.util.ArrayList JavaDoc;
39 import java.util.HashMap JavaDoc;
40 import java.util.logging.Level JavaDoc;
41 import java.util.logging.Logger JavaDoc;
42 import java.util.regex.Matcher JavaDoc;
43 import java.util.regex.Pattern JavaDoc;
44
45 /**
46  * The generator for the web-app deploy
47  */

48 public class HostRegexpDeployGenerator extends DeployGenerator<HostController> {
49   private static final Logger JavaDoc log = Log.open(HostSingleDeployGenerator.class);
50
51   private HostContainer _container;
52
53   private HostConfig _config;
54   
55   private ArrayList JavaDoc<HostConfig> _hostDefaults
56     = new ArrayList JavaDoc<HostConfig>();
57
58   private ArrayList JavaDoc<HostController> _entries
59     = new ArrayList JavaDoc<HostController>();
60
61   /**
62    * Creates the new host deploy.
63    */

64   public HostRegexpDeployGenerator(DeployContainer<HostController> container)
65   {
66     super(container);
67   }
68
69   /**
70    * Creates the new host deploy.
71    */

72   public HostRegexpDeployGenerator(DeployContainer<HostController> container,
73               HostContainer hostContainer,
74               HostConfig config)
75   {
76     super(container);
77     
78     setContainer(hostContainer);
79
80     _config = config;
81   }
82
83   /**
84    * Gets the application container.
85    */

86   public HostContainer getContainer()
87   {
88     return _container;
89   }
90
91   /**
92    * Sets the application container.
93    */

94   public void setContainer(HostContainer container)
95   {
96     _container = container;
97   }
98   
99   /**
100    * Returns the current array of application entries.
101    */

102   public HostController generateController(String JavaDoc name)
103   {
104     Pattern JavaDoc regexp = _config.getRegexp();
105     Matcher JavaDoc matcher = regexp.matcher(name);
106
107     if (! matcher.find() || matcher.start() != 0)
108       return null;
109
110     Thread JavaDoc thread = Thread.currentThread();
111     ClassLoader JavaDoc oldLoader = thread.getContextClassLoader();
112
113     try {
114       thread.setContextClassLoader(getParentClassLoader());
115       
116       int length = matcher.end() - matcher.start();
117
118       String JavaDoc hostName = matcher.group();
119         
120       ArrayList JavaDoc<String JavaDoc> vars = new ArrayList JavaDoc<String JavaDoc>();
121
122       HashMap JavaDoc<String JavaDoc,Object JavaDoc> varMap = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
123         
124       for (int j = 0; j <= matcher.groupCount(); j++) {
125     vars.add(matcher.group(j));
126     varMap.put("host" + j, matcher.group(j));
127       }
128
129       varMap.put("regexp", vars);
130
131       if (_config.getHostName() != null) {
132     try {
133       hostName = Config.evalString(_config.getHostName(), varMap);
134     } catch (Exception JavaDoc e) {
135       log.log(Level.WARNING, e.toString(), e);
136     }
137       }
138
139       HostController controller
140     = new HostController(name, _config, _container, varMap);
141
142       controller.setRegexpName(name);
143
144       controller.setRegexp(regexp);
145       controller.setRootDirectoryPattern(_config.getRootDirectory());
146
147       // XXX: not dynamic-deploy in the sense that the mappings are known
148
//controller.setDynamicDeploy(true);
149
//controller.setRegexpValues(vars);
150
//controller.setHostConfig(_config);
151
// _controller.setJarPath(_archivePath);
152

153       for (int i = 0; i < _hostDefaults.size(); i++)
154     controller.addConfigDefault(_hostDefaults.get(i));
155
156       controller.init();
157     
158       Path rootDir = controller.getRootDirectory();
159
160       if (rootDir == null || ! rootDir.canRead())
161     return null;
162     
163       synchronized (_entries) {
164     for (int i = 0; i < _entries.size(); i++) {
165       HostController oldController = _entries.get(i);
166
167       if (rootDir.equals(oldController.getRootDirectory()))
168         return oldController;
169     }
170       
171     _entries.add(controller);
172       }
173
174       // registers mbean
175
/*
176       try {
177     controller.deployHost();
178       } catch (Exception e) {
179     log.log(Level.WARNING, e.toString(), e);
180       }
181       */

182
183       return controller;
184     } finally {
185       thread.setContextClassLoader(oldLoader);
186     }
187   }
188
189   public String JavaDoc toString()
190   {
191     return "HostRegexpDeployGenerator[" + _config + "]";
192   }
193 }
194
Popular Tags