001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.config; 020 021import java.io.IOException; 022import java.io.PrintWriter; 023import java.util.HashMap; 024import java.util.Map; 025 026import lucee.commons.io.IOUtil; 027import lucee.commons.io.SystemUtil; 028import lucee.commons.io.res.Resource; 029import lucee.commons.io.res.type.file.FileResource; 030import lucee.commons.io.res.util.ResourceUtil; 031import lucee.commons.lang.ClassException; 032import lucee.commons.lang.SystemOut; 033import lucee.runtime.CFMLFactory; 034import lucee.runtime.engine.CFMLEngineImpl; 035import lucee.runtime.exp.PageException; 036import lucee.transformer.library.function.FunctionLibException; 037import lucee.transformer.library.tag.TagLibException; 038 039import org.w3c.dom.Document; 040import org.w3c.dom.Element; 041import org.xml.sax.SAXException; 042 043import com.jacob.com.LibraryLoader; 044 045 046/** 047 * 048 */ 049public final class ConfigServerFactory extends ConfigFactory{ 050 051 /** 052 * creates a new ServletConfig Impl Object 053 * @param engine 054 * @param initContextes 055 * @param contextes 056 * @param configDir 057 * @return new Instance 058 * @throws SAXException 059 * @throws ClassNotFoundException 060 * @throws PageException 061 * @throws IOException 062 * @throws TagLibException 063 * @throws FunctionLibException 064 */ 065 public static ConfigServerImpl newInstance(CFMLEngineImpl engine,Map<String,CFMLFactory> initContextes, Map<String,CFMLFactory> contextes, Resource configDir) 066 throws SAXException, ClassException, PageException, IOException, TagLibException, FunctionLibException { 067 068 boolean isCLI=SystemUtil.isCLICall(); 069 if(isCLI){ 070 Resource logs = configDir.getRealResource("logs"); 071 logs.mkdirs(); 072 Resource out = logs.getRealResource("out"); 073 Resource err = logs.getRealResource("err"); 074 ResourceUtil.touch(out); 075 ResourceUtil.touch(err); 076 if(logs instanceof FileResource) { 077 SystemUtil.setPrintWriter(SystemUtil.OUT, new PrintWriter((FileResource)out)); 078 SystemUtil.setPrintWriter(SystemUtil.ERR, new PrintWriter((FileResource)err)); 079 } 080 else{ 081 SystemUtil.setPrintWriter(SystemUtil.OUT, new PrintWriter(IOUtil.getWriter(out,"UTF-8"))); 082 SystemUtil.setPrintWriter(SystemUtil.ERR, new PrintWriter(IOUtil.getWriter(err,"UTF-8"))); 083 } 084 } 085 SystemOut.print(SystemUtil.getPrintWriter(SystemUtil.OUT), 086 "===================================================================\n"+ 087 "SERVER CONTEXT\n" + 088 "-------------------------------------------------------------------\n"+ 089 "- config:"+configDir+"\n"+ 090 "- loader-version:"+SystemUtil.getLoaderVersion()+"\n"+ 091 "===================================================================\n" 092 093 ); 094 095 boolean doNew=doNew(configDir); 096 097 Resource configFile=configDir.getRealResource("lucee-server.xml"); 098 if(!configFile.exists()) { 099 configFile.createFile(true); 100 //InputStream in = new TextFile("").getClass().getResourceAsStream("/resource/config/server.xml"); 101 createFileFromResource( 102 "/resource/config/server.xml", 103 configFile.getAbsoluteResource(), 104 "tpiasfap" 105 ); 106 } 107 //print.out(configFile); 108 Document doc=loadDocument(configFile); 109 110 ConfigServerImpl config=new ConfigServerImpl(engine,initContextes,contextes,configDir,configFile); 111 load(config,doc,false,doNew); 112 113 createContextFiles(configDir,config,doNew); 114 return config; 115 } 116 /** 117 * reloads the Config Object 118 * @param configServer 119 * @throws SAXException 120 * @throws ClassNotFoundException 121 * @throws PageException 122 * @throws IOException 123 * @throws TagLibException 124 * @throws FunctionLibException 125 */ 126 public static void reloadInstance(ConfigServerImpl configServer) 127 throws SAXException, ClassException, PageException, IOException, TagLibException, FunctionLibException { 128 Resource configFile=configServer.getConfigFile(); 129 130 if(configFile==null) return ; 131 if(second(configServer.getLoadTime())>second(configFile.lastModified())) return ; 132 boolean doNew=doNew(configServer.getConfigDir()); 133 load(configServer,loadDocument(configFile),true,doNew); 134 } 135 136 private static long second(long ms) { 137 return ms/1000; 138 } 139 140 /** 141 * @param configServer 142 * @param doc 143 * @throws ClassNotFoundException 144 * @throws IOException 145 * @throws FunctionLibException 146 * @throws TagLibException 147 * @throws PageException 148 */ 149 static void load(ConfigServerImpl configServer, Document doc, boolean isReload, boolean doNew) throws ClassException, PageException, IOException, TagLibException, FunctionLibException { 150 ConfigWebFactory.load(null,configServer,doc, isReload,doNew); 151 loadLabel(configServer,doc); 152 } 153 154 155 private static void loadLabel(ConfigServerImpl configServer, Document doc) { 156 Element el= getChildByName(doc.getDocumentElement(),"labels"); 157 Element[] children=getChildren(el,"label"); 158 159 Map<String, String> labels=new HashMap<String, String>(); 160 if(children!=null)for(int i=0;i<children.length;i++) { 161 el=children[i]; 162 163 String id=el.getAttribute("id"); 164 String name=el.getAttribute("name"); 165 if(id!=null && name!=null) { 166 labels.put(id, name); 167 } 168 } 169 configServer.setLabels(labels); 170 } 171 172 public static void createContextFiles(Resource configDir, ConfigServer config, boolean doNew) { 173 174 Resource contextDir = configDir.getRealResource("context"); 175 Resource adminDir = contextDir.getRealResource("admin"); 176 177 178 179 // Debug 180 Resource debug = adminDir.getRealResource("debug"); 181 create("/resource/context/admin/debug/",new String[]{ 182 "Debug.cfc","Field.cfc","Group.cfc","Classic.cfc","Modern.cfc","Comment.cfc" 183 },debug,doNew); 184 185 186 // DB Drivers types 187 Resource dbDir = adminDir.getRealResource("dbdriver"); 188 Resource typesDir = dbDir.getRealResource("types"); 189 create("/resource/context/admin/dbdriver/types/",new String[]{ 190 "IDriver.cfc","Driver.cfc","IDatasource.cfc","IDriverSelector.cfc","Field.cfc" 191 },typesDir,doNew); 192 193 // DB Drivers 194 create("/resource/context/admin/dbdriver/",new String[]{ 195 "H2.cfc","H2Selector.cfc","H2Server.cfc","HSQLDB.cfc","MSSQL.cfc","MSSQL2.cfc","MSSQLSelector.cfc","DB2.cfc","Oracle.cfc" 196 ,"MySQL.cfc","ODBC.cfc","Sybase.cfc","PostgreSql.cfc","Other.cfc","Firebird.cfc"} 197 ,dbDir,doNew); 198 199 // Cache Drivers 200 Resource cDir = adminDir.getRealResource("cdriver"); 201 create("/resource/context/admin/cdriver/",new String[]{ 202 "Cache.cfc","RamCache.cfc","EHCache.cfc","Field.cfc","Group.cfc"} 203 ,cDir,doNew); 204 205 delete(cDir,new String[]{"EHCacheLite.cfc"}); 206 207 Resource wcdDir = configDir.getRealResource("web-context-deployment/admin"); 208 Resource cdDir = wcdDir.getRealResource("cdriver"); 209 delete(cdDir,new String[]{"EHCache.cfc","EHCacheLite.cfc"}); 210 try { 211 ResourceUtil.removeEmptyFolders(wcdDir,null); 212 } 213 catch (IOException e) { 214 // TODO Auto-generated catch block 215 e.printStackTrace(); 216 } 217 218 219 // Gateway Drivers 220 Resource gDir = adminDir.getRealResource("gdriver"); 221 create("/resource/context/admin/gdriver/",new String[]{ 222 "TaskGatewayDriver.cfc","DirectoryWatcher.cfc","MailWatcher.cfc","Gateway.cfc","Field.cfc","Group.cfc"} 223 ,gDir,doNew); 224 225 // Logging/appender 226 Resource app = adminDir.getRealResource("logging/appender"); 227 create("/resource/context/admin/logging/appender/",new String[]{ 228 "ConsoleAppender.cfc","ResourceAppender.cfc","Appender.cfc","Field.cfc","Group.cfc"} 229 ,app,doNew); 230 231 // Logging/layout 232 Resource lay = adminDir.getRealResource("logging/layout"); 233 create("/resource/context/admin/logging/layout/",new String[]{ 234 "ClassicLayout.cfc","HTMLLayout.cfc","PatternLayout.cfc","XMLLayout.cfc","Layout.cfc","Field.cfc","Group.cfc"} 235 ,lay,doNew); 236 237 // Security 238 Resource secDir = configDir.getRealResource("security"); 239 if(!secDir.exists())secDir.mkdirs(); 240 Resource res = create("/resource/security/","cacerts",secDir,false); 241 System.setProperty("javax.net.ssl.trustStore",res.toString()); 242 243 // ESAPI 244 Resource propDir = configDir.getRealResource("properties"); 245 if(!propDir.exists())propDir.mkdirs(); 246 create("/resource/properties/","ESAPI.properties",propDir,doNew); 247 System.setProperty("org.owasp.esapi.resources", propDir.toString()); 248 249 250 // Jacob 251 if (SystemUtil.isWindows()) { 252 253 Resource binDir = configDir.getRealResource("bin"); 254 if (binDir != null) { 255 256 if (!binDir.exists()) 257 binDir.mkdirs(); 258 259 String name = (SystemUtil.getJREArch() == SystemUtil.ARCH_64) ? "jacob-x64.dll" : "jacob-x86.dll"; 260 261 Resource jacob = binDir.getRealResource(name); 262 if (!jacob.exists()) { 263 createFileFromResourceEL("/resource/bin/" + name, jacob); 264 } 265 // SystemOut.printDate(SystemUtil.PRINTWRITER_OUT,"set-property -> "+LibraryLoader.JACOB_DLL_PATH+":"+jacob.getAbsolutePath()); 266 System.setProperty(LibraryLoader.JACOB_DLL_PATH, jacob.getAbsolutePath()); 267 // SystemOut.printDate(SystemUtil.PRINTWRITER_OUT,"set-property -> "+LibraryLoader.JACOB_DLL_NAME+":"+name); 268 System.setProperty(LibraryLoader.JACOB_DLL_NAME, name); 269 } 270 } 271 } 272 273}