1 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 ; 39 import java.util.HashMap ; 40 import java.util.logging.Level ; 41 import java.util.logging.Logger ; 42 import java.util.regex.Matcher ; 43 import java.util.regex.Pattern ; 44 45 48 public class HostRegexpDeployGenerator extends DeployGenerator<HostController> { 49 private static final Logger log = Log.open(HostSingleDeployGenerator.class); 50 51 private HostContainer _container; 52 53 private HostConfig _config; 54 55 private ArrayList <HostConfig> _hostDefaults 56 = new ArrayList <HostConfig>(); 57 58 private ArrayList <HostController> _entries 59 = new ArrayList <HostController>(); 60 61 64 public HostRegexpDeployGenerator(DeployContainer<HostController> container) 65 { 66 super(container); 67 } 68 69 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 86 public HostContainer getContainer() 87 { 88 return _container; 89 } 90 91 94 public void setContainer(HostContainer container) 95 { 96 _container = container; 97 } 98 99 102 public HostController generateController(String name) 103 { 104 Pattern regexp = _config.getRegexp(); 105 Matcher matcher = regexp.matcher(name); 106 107 if (! matcher.find() || matcher.start() != 0) 108 return null; 109 110 Thread thread = Thread.currentThread(); 111 ClassLoader oldLoader = thread.getContextClassLoader(); 112 113 try { 114 thread.setContextClassLoader(getParentClassLoader()); 115 116 int length = matcher.end() - matcher.start(); 117 118 String hostName = matcher.group(); 119 120 ArrayList <String > vars = new ArrayList <String >(); 121 122 HashMap <String ,Object > varMap = new HashMap <String ,Object >(); 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 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 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 182 183 return controller; 184 } finally { 185 thread.setContextClassLoader(oldLoader); 186 } 187 } 188 189 public String toString() 190 { 191 return "HostRegexpDeployGenerator[" + _config + "]"; 192 } 193 } 194 | Popular Tags |