| Revision | 13 (tree) |
|---|---|
| Time | 2015-08-17 15:00:31 |
| Author | t_nakayama1971 |
(empty log message)
| @@ -0,0 +1,211 @@ | ||
| 1 | +package org.apache.catalina.loader; | |
| 2 | + | |
| 3 | +import java.io.File; | |
| 4 | +import java.io.FileFilter; | |
| 5 | +import java.io.FileReader; | |
| 6 | +import java.io.IOException; | |
| 7 | +import java.io.LineNumberReader; | |
| 8 | +import java.net.URI; | |
| 9 | +import java.util.ArrayList; | |
| 10 | +import java.util.List; | |
| 11 | + | |
| 12 | +import javax.servlet.ServletContext; | |
| 13 | + | |
| 14 | +import org.apache.catalina.Context; | |
| 15 | +import org.apache.catalina.Globals; | |
| 16 | +import org.apache.catalina.LifecycleException; | |
| 17 | + | |
| 18 | +/** | |
| 19 | + * @author Martin Kahr | |
| 20 | + * | |
| 21 | + */ | |
| 22 | +public final class DevLoader extends WebappLoader { | |
| 23 | + /** Information */ | |
| 24 | + public static final String INFO = "org.apache.catalina.loader.DevLoader/1.0"; | |
| 25 | + | |
| 26 | + /** | |
| 27 | + * コンストラクタ | |
| 28 | + */ | |
| 29 | + public DevLoader() { | |
| 30 | + super(); | |
| 31 | + } | |
| 32 | + | |
| 33 | + /** | |
| 34 | + * コンストラクタ | |
| 35 | + * @param parent 親クラスローダ | |
| 36 | + */ | |
| 37 | + public DevLoader(final ClassLoader parent) { | |
| 38 | + super(parent); | |
| 39 | + } | |
| 40 | + | |
| 41 | + /** | |
| 42 | + * @see org.apache.catalina.util.LifecycleBase#startInternal() | |
| 43 | + */ | |
| 44 | + @Override | |
| 45 | + protected void startInternal() throws LifecycleException { | |
| 46 | + log("Starting DevLoader"); | |
| 47 | + | |
| 48 | + super.startInternal(); | |
| 49 | + | |
| 50 | + ClassLoader cl = super.getClassLoader(); | |
| 51 | + if (!(cl instanceof WebappClassLoader)) { | |
| 52 | + logError("Unable to install WebappClassLoader !"); | |
| 53 | + return; | |
| 54 | + } | |
| 55 | + WebappClassLoader devCl = (WebappClassLoader) cl; | |
| 56 | + | |
| 57 | + StringBuilder classpath = new StringBuilder(); | |
| 58 | + for (final String entry : readWebClassPathEntries()) { | |
| 59 | + File f = new File(entry); | |
| 60 | + if (f.exists()) { | |
| 61 | + if (f.isDirectory() && !entry.endsWith("/")) { | |
| 62 | + f = new File(entry + "/"); | |
| 63 | + } | |
| 64 | + URI url = f.toURI(); | |
| 65 | + devCl.addRepository(url.toString()); | |
| 66 | + classpath.append(f.toString() + File.pathSeparator); | |
| 67 | + log("added " + url.toString()); | |
| 68 | + } else { | |
| 69 | + logError(entry + " does not exist !"); | |
| 70 | + } | |
| 71 | + } | |
| 72 | + | |
| 73 | + String cp = (String)getServletContext().getAttribute(Globals.CLASS_PATH_ATTR); | |
| 74 | + for (final String token : cp.split(File.pathSeparator)) { | |
| 75 | + // only on windows | |
| 76 | + if (token.charAt(0) == '/' && token.charAt(2) == ':') { | |
| 77 | + classpath.append(token.substring(1) + File.pathSeparator); | |
| 78 | + } else { | |
| 79 | + classpath.append(token + File.pathSeparator); | |
| 80 | + } | |
| 81 | + } | |
| 82 | + | |
| 83 | + getServletContext().setAttribute(Globals.CLASS_PATH_ATTR, classpath.toString()); | |
| 84 | + log("JSPCompiler Classpath = " + classpath); | |
| 85 | + } | |
| 86 | + | |
| 87 | + /** | |
| 88 | + * ログ出力 | |
| 89 | + * @param msg 出力メッセージ | |
| 90 | + */ | |
| 91 | + protected void log(final String msg) { | |
| 92 | + System.out.println("[DevLoader] " + msg); | |
| 93 | + } | |
| 94 | + | |
| 95 | + /** | |
| 96 | + * エラー出力 | |
| 97 | + * @param msg エラーメッセージ | |
| 98 | + */ | |
| 99 | + protected void logError(final String msg) { | |
| 100 | + System.err.println("[DevLoader] Error: " + msg); | |
| 101 | + } | |
| 102 | + | |
| 103 | + /** | |
| 104 | + * Webクラスパス読込 | |
| 105 | + * @return Webクラスパス行 | |
| 106 | + */ | |
| 107 | + protected List<String> readWebClassPathEntries() { | |
| 108 | + File prjDir = getProjectRootDir(); | |
| 109 | + if (prjDir == null) { | |
| 110 | + return new ArrayList<String>(); | |
| 111 | + } | |
| 112 | + log("projectdir=" + prjDir.getAbsolutePath()); | |
| 113 | + | |
| 114 | + return loadWebClassPathFile(prjDir); | |
| 115 | + } | |
| 116 | + | |
| 117 | + /** | |
| 118 | + * プロジェクトルート取得 | |
| 119 | + * @return プロジェクトルート | |
| 120 | + */ | |
| 121 | + protected File getProjectRootDir() { | |
| 122 | + File rootDir = getWebappDir(); | |
| 123 | + FileFilter filter = new LoaderFilter(); | |
| 124 | + while (rootDir != null) { | |
| 125 | + File[] files = rootDir.listFiles(filter); | |
| 126 | + if (files != null && files.length >= 1) { | |
| 127 | + return files[0].getParentFile(); | |
| 128 | + } | |
| 129 | + rootDir = rootDir.getParentFile(); | |
| 130 | + } | |
| 131 | + return null; | |
| 132 | + } | |
| 133 | + | |
| 134 | + /** | |
| 135 | + * フィルタ | |
| 136 | + * @author Tadashi Nakayama | |
| 137 | + */ | |
| 138 | + static class LoaderFilter implements FileFilter { | |
| 139 | + /** クラスパスファイル */ | |
| 140 | + static final String WEB_CLASS_PATH_FILE = ".#webclasspath"; | |
| 141 | + /** プラグインファイル */ | |
| 142 | + static final String TOMCAT_PLUGIN_FILE = ".tomcatplugin"; | |
| 143 | + | |
| 144 | + /** | |
| 145 | + * @see java.io.FileFilter#accept(java.io.File) | |
| 146 | + */ | |
| 147 | + @Override | |
| 148 | + public boolean accept(final File file) { | |
| 149 | + return (file.getName().equalsIgnoreCase(WEB_CLASS_PATH_FILE) | |
| 150 | + || file.getName().equalsIgnoreCase(TOMCAT_PLUGIN_FILE)); | |
| 151 | + } | |
| 152 | + } | |
| 153 | + | |
| 154 | + /** | |
| 155 | + * Webクラスパスファイル読込 | |
| 156 | + * @param prjDir プロジェクトディレクトリ | |
| 157 | + * @return 読込行リスト | |
| 158 | + */ | |
| 159 | + protected List<String> loadWebClassPathFile(final File prjDir) { | |
| 160 | + File cpFile = new File(prjDir, LoaderFilter.WEB_CLASS_PATH_FILE); | |
| 161 | + if (cpFile.exists()) { | |
| 162 | + FileReader reader = null; | |
| 163 | + LineNumberReader lr = null; | |
| 164 | + try { | |
| 165 | + List<String> rc = new ArrayList<String>(); | |
| 166 | + reader = new FileReader(cpFile); | |
| 167 | + lr = new LineNumberReader(reader); | |
| 168 | + String line = null; | |
| 169 | + while ((line = lr.readLine()) != null) { | |
| 170 | + line = line.replace('\\', '/'); | |
| 171 | + rc.add(line); | |
| 172 | + } | |
| 173 | + return rc; | |
| 174 | + } catch (IOException ex) { | |
| 175 | + logError(ex.getMessage()); | |
| 176 | + } finally { | |
| 177 | + if (lr != null) { | |
| 178 | + try { | |
| 179 | + lr.close(); | |
| 180 | + } catch (IOException ex) { | |
| 181 | + logError(ex.getMessage()); | |
| 182 | + } | |
| 183 | + } | |
| 184 | + if (reader != null) { | |
| 185 | + try { | |
| 186 | + reader.close(); | |
| 187 | + } catch (IOException ex) { | |
| 188 | + logError(ex.getMessage()); | |
| 189 | + } | |
| 190 | + } | |
| 191 | + } | |
| 192 | + } | |
| 193 | + return null; | |
| 194 | + } | |
| 195 | + | |
| 196 | + /** | |
| 197 | + * サーブレットコンテキスト取得 | |
| 198 | + * @return サーブレットコンテキスト | |
| 199 | + */ | |
| 200 | + protected ServletContext getServletContext() { | |
| 201 | + return ((Context) getContainer()).getServletContext(); | |
| 202 | + } | |
| 203 | + | |
| 204 | + /** | |
| 205 | + * Webアプリディレクトリ取得 | |
| 206 | + * @return Webアプリディレクトリ | |
| 207 | + */ | |
| 208 | + protected File getWebappDir() { | |
| 209 | + return new File(getServletContext().getRealPath("/")); | |
| 210 | + } | |
| 211 | +} |