KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > make > MakeLoader


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.make;
30
31 import com.caucho.config.DynamicBean;
32 import com.caucho.config.DynamicItem;
33 import com.caucho.loader.DynamicClassLoader;
34 import com.caucho.loader.EnvironmentLocal;
35 import com.caucho.loader.Loader;
36 import com.caucho.log.Log;
37 import com.caucho.server.util.CauchoSystem;
38 import com.caucho.util.CharBuffer;
39 import com.caucho.vfs.Dependency;
40 import com.caucho.vfs.JarPath;
41 import com.caucho.vfs.Path;
42
43 import java.util.ArrayList JavaDoc;
44 import java.util.logging.Level JavaDoc;
45 import java.util.logging.Logger JavaDoc;
46
47 /**
48  * Class loader which checks for changes in class files and automatically
49  * picks up new jars.
50  */

51 public class MakeLoader extends Loader implements DynamicBean, Make {
52   private static final Logger JavaDoc log = Log.open(MakeLoader.class);
53
54   private static final EnvironmentLocal<DynamicItem[]> _localConfig =
55     new EnvironmentLocal<DynamicItem[]>();
56
57   private static DynamicItem []_configItems;
58     
59   // The class directory
60
private Path _path;
61
62   // The task list
63
private ArrayList JavaDoc<Dependency> _dependList = new ArrayList JavaDoc<Dependency>();
64   private ArrayList JavaDoc<Make> _makeList = new ArrayList JavaDoc<Make>();
65
66   /**
67    * Null constructor for the make loader.
68    */

69   public MakeLoader()
70   {
71   }
72
73   public DynamicItem []getDynamicConfigurationElements()
74   {
75     DynamicItem []configItems = _localConfig.get();
76
77     if (configItems == null) {
78       ArrayList JavaDoc<DynamicItem> items = new ArrayList JavaDoc<DynamicItem>();
79
80       /*
81       items.add(new DynamicItem("javac", JavacTask.class, "make"));
82       items.add(new DynamicItem("doclet", DocletTask.class, "make"));
83       */

84
85       ClassLoader JavaDoc loader = Thread.currentThread().getContextClassLoader();
86       
87       try {
88     Class JavaDoc taskClass = Class.forName("com.caucho.ejb.doclet.EjbDocletTask",
89                     false,
90                     loader);
91
92     items.add(new DynamicItem("ejb-doclet", taskClass, "make"));
93       } catch (Throwable JavaDoc e) {
94     log.log(Level.FINEST, e.toString(), e);
95       }
96
97       configItems = items.toArray(new DynamicItem[items.size()]);
98
99       _localConfig.set(configItems);
100     }
101     
102     return configItems;
103   }
104
105   /**
106    * Sets the resource directory.
107    */

108   public void setPath(Path path)
109   {
110     _path = path;
111   }
112
113   /**
114    * Gets the resource path.
115    */

116   public Path getPath()
117   {
118     return _path;
119   }
120
121   /**
122    * Sets the owning class loader.
123    */

124   public void setLoader(DynamicClassLoader loader)
125   {
126     super.setLoader(loader);
127
128     loader.addURL(_path);
129   }
130
131   /**
132    * Given a class or resource name, returns a patch to that resource.
133    *
134    * @param name the class or resource name.
135    *
136    * @return the path representing the class or resource.
137    */

138   public Path getPath(String JavaDoc name)
139   {
140     for (int i = 0; i < _makeList.size(); i++) {
141       Make make = _makeList.get(i);
142
143       try {
144         make.make();
145       } catch (Exception JavaDoc e) {
146         throw new RuntimeException JavaDoc(e);
147       }
148     }
149     
150     return _path.lookup(name);
151   }
152
153   /**
154    * Sets the make config.
155    */

156   public void addTask(TaskConfig task)
157   {
158     Object JavaDoc obj = task.getTask();
159
160     if (obj instanceof Make)
161       _makeList.add((Make) obj);
162   }
163
164   /**
165    * Sets the make config.
166    */

167   public void addMake(Make make)
168   {
169     _makeList.add(make);
170   }
171
172   /**
173    * Makes the loader.
174    */

175   public void make()
176     throws Exception JavaDoc
177   {
178   }
179
180   /**
181    * Adds the class of this resource.
182    */

183   protected String JavaDoc getClassPath(String JavaDoc head)
184   {
185     CharBuffer cb = new CharBuffer();
186
187     if (! head.equals("")) {
188       cb.append(head);
189       cb.append(CauchoSystem.getPathSeparatorChar());
190     }
191     
192     if (_path instanceof JarPath)
193       cb.append(((JarPath) _path).getContainer().getNativePath());
194     else if (_path.isDirectory())
195       cb.append(_path.getNativePath());
196
197     return cb.toString();
198   }
199
200   /**
201    * Returns a printable representation of the loader.
202    */

203   public String JavaDoc toString()
204   {
205     return "MakeLoader[" + _path + "]";
206   }
207
208   static {
209   };
210 }
211
212
213
Popular Tags