001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.java.gen;
030:
031: import com.caucho.java.JavaWriter;
032: import com.caucho.server.util.CauchoSystem;
033: import com.caucho.util.L10N;
034: import com.caucho.vfs.Depend;
035: import com.caucho.vfs.Path;
036: import com.caucho.vfs.PersistentDependency;
037: import com.caucho.vfs.Vfs;
038:
039: import java.io.IOException;
040: import java.util.ArrayList;
041:
042: /**
043: * Basic method generation.
044: */
045: public class DependencyComponent extends ClassComponent {
046: private static final L10N L = new L10N(DependencyComponent.class);
047:
048: private String _initMethod = "_caucho_init";
049: private String _isModifiedMethod = "_caucho_is_modified";
050:
051: private Path _searchPath;
052:
053: private ArrayList<PersistentDependency> _dependList = new ArrayList<PersistentDependency>();
054:
055: /**
056: * Sets the search path.
057: */
058: public void setSearchPath(Path searchPath) {
059: _searchPath = searchPath;
060: }
061:
062: /**
063: * Adds a dependency list.
064: */
065: public void addDependencyList(
066: ArrayList<PersistentDependency> dependList) {
067: for (int i = 0; i < dependList.size(); i++)
068: addDependency(dependList.get(i));
069: }
070:
071: /**
072: * Adds a dependency.
073: */
074: public void addDependency(PersistentDependency depend) {
075: if (!_dependList.contains(depend))
076: _dependList.add(depend);
077: }
078:
079: /**
080: * Generates the code for the dependencies.
081: *
082: * @param out the writer to the output stream.
083: */
084: public void generate(JavaWriter out) throws IOException {
085: out
086: .println("private static com.caucho.vfs.Dependency []_caucho_depend;");
087:
088: out.println();
089: out.println("public static void " + _initMethod
090: + "(com.caucho.vfs.Path path)");
091: out.println("{");
092: out.pushDepth();
093:
094: out.println("_caucho_depend = new com.caucho.vfs.Dependency["
095: + _dependList.size() + "];");
096:
097: Path searchPath = _searchPath;
098:
099: for (int i = 0; i < _dependList.size(); i++) {
100: PersistentDependency dependency = _dependList.get(i);
101:
102: if (dependency instanceof Depend) {
103: Depend depend = (Depend) _dependList.get(i);
104: Path path = depend.getPath();
105:
106: out.print("_caucho_depend[" + i
107: + "] = new com.caucho.vfs.Depend(");
108:
109: // php/3b33
110: String relativePath;
111: if (searchPath != null)
112: relativePath = searchPath.lookup(
113: path.getRelativePath()).getRelativePath();
114: else {
115: String fullPath = path.getFullPath();
116: String pwd = Vfs.lookup().getFullPath();
117:
118: if (fullPath.startsWith(pwd))
119: relativePath = "./"
120: + fullPath.substring(pwd.length());
121: else
122: relativePath = fullPath;
123: }
124:
125: out.print("path.lookup(\"" + relativePath + "\"), ");
126:
127: out.println(depend.getDigest() + "L, "
128: + depend.getRequireSource() + ");");
129: } else {
130: out.print("_caucho_depend[" + i + "] = ");
131: out.print(dependency.getJavaCreateString());
132: out.println(";");
133: }
134: }
135:
136: out.popDepth();
137: out.println("}");
138:
139: out.println();
140: out
141: .println("public static boolean " + _isModifiedMethod
142: + "()");
143: out.println("{");
144: out.pushDepth();
145:
146: printVersionChange(out);
147:
148: out
149: .println("for (int i = _caucho_depend.length - 1; i >= 0; i--) {");
150: out.println(" if (_caucho_depend[i].isModified())");
151: out.println(" return true;");
152: out.println("}");
153:
154: out.println();
155: out.println("return false;");
156:
157: out.popDepth();
158: out.println("}");
159: }
160:
161: /**
162: * Prints code to detect a version change.
163: */
164: protected void printVersionChange(JavaWriter out)
165: throws IOException {
166: out
167: .println("if (com.caucho.server.util.CauchoSystem.getVersionId() != "
168: + "0x"
169: + Long.toHexString(CauchoSystem.getVersionId())
170: + "L)");
171: out.println(" return true;");
172: }
173: }
|