KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > java > gen > DependencyComponent


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

28
29 package com.caucho.java.gen;
30
31 import com.caucho.java.JavaWriter;
32 import com.caucho.server.util.CauchoSystem;
33 import com.caucho.util.L10N;
34 import com.caucho.vfs.Depend;
35 import com.caucho.vfs.Path;
36 import com.caucho.vfs.PersistentDependency;
37
38 import java.io.IOException JavaDoc;
39 import java.util.ArrayList JavaDoc;
40
41 /**
42  * Basic method generation.
43  */

44 public class DependencyComponent extends ClassComponent {
45   private static final L10N L = new L10N(DependencyComponent.class);
46
47   private String JavaDoc _initMethod = "_caucho_init";
48   private String JavaDoc _isModifiedMethod = "_caucho_is_modified";
49   
50   private Path _searchPath;
51
52   private ArrayList JavaDoc<PersistentDependency> _dependList =
53     new ArrayList JavaDoc<PersistentDependency>();
54
55   /**
56    * Sets the search path.
57    */

58   public void setSearchPath(Path searchPath)
59   {
60     _searchPath = searchPath;
61   }
62
63   /**
64    * Adds a dependency list.
65    */

66   public void addDependencyList(ArrayList JavaDoc<PersistentDependency> dependList)
67   {
68     for (int i = 0; i < dependList.size(); i++)
69       addDependency(dependList.get(i));
70   }
71
72   /**
73    * Adds a dependency.
74    */

75   public void addDependency(PersistentDependency depend)
76   {
77     if (! _dependList.contains(depend))
78       _dependList.add(depend);
79   }
80   
81   /**
82    * Generates the code for the dependencies.
83    *
84    * @param out the writer to the output stream.
85    */

86   public void generate(JavaWriter out)
87     throws IOException JavaDoc
88   {
89     out.println("private static com.caucho.vfs.Dependency []_caucho_depend;");
90     
91     out.println();
92     out.println("public static void " + _initMethod + "(com.caucho.vfs.Path path)");
93     out.println("{");
94     out.pushDepth();
95
96     out.println("_caucho_depend = new com.caucho.vfs.Dependency[" +
97         _dependList.size() + "];");
98
99     Path searchPath = _searchPath;
100
101     if (searchPath == null)
102       searchPath = JavaClassGenerator.getDefaultSearchPath();
103     
104     for (int i = 0; i < _dependList.size(); i++) {
105       PersistentDependency dependency = _dependList.get(i);
106
107       if (dependency instanceof Depend) {
108         Depend depend = (Depend) _dependList.get(i);
109         Path path = depend.getPath();
110
111         out.print("_caucho_depend[" + i + "] = new com.caucho.vfs.Depend(");
112
113     String JavaDoc relativePath;
114     relativePath = searchPath.lookup(path.getRelativePath()).getRelativePath();
115     out.print("path.lookup(\"" + relativePath + "\"), ");
116       
117     out.println(depend.getDigest() + "L, " +
118             depend.getRequireSource() + ");");
119       }
120       else {
121         out.print("_caucho_depend[" + i + "] = ");
122         out.print(dependency.getJavaCreateString());
123         out.println(";");
124       }
125     }
126
127     out.popDepth();
128     out.println("}");
129
130     out.println();
131     out.println("public static boolean " + _isModifiedMethod + "()");
132     out.println("{");
133     out.pushDepth();
134
135     printVersionChange(out);
136
137     out.println("for (int i = _caucho_depend.length - 1; i >= 0; i--) {");
138     out.println(" if (_caucho_depend[i].isModified())");
139     out.println(" return true;");
140     out.println("}");
141
142     out.println();
143     out.println("return false;");
144     
145     out.popDepth();
146     out.println("}");
147   }
148
149   /**
150    * Prints code to detect a version change.
151    */

152   protected void printVersionChange(JavaWriter out)
153     throws IOException JavaDoc
154   {
155     out.println("if (com.caucho.server.util.CauchoSystem.getVersionId() != " +
156         "0x" + Long.toHexString(CauchoSystem.getVersionId()) + "L)");
157     out.println(" return true;");
158   }
159 }
160
Popular Tags