001: // Copyright 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.integration.app1.pages;
016:
017: import java.io.IOException;
018: import java.io.InputStream;
019: import java.io.InputStreamReader;
020: import java.net.JarURLConnection;
021: import java.net.URL;
022: import java.net.URLConnection;
023: import java.util.Enumeration;
024: import java.util.List;
025: import java.util.jar.JarEntry;
026:
027: import org.apache.tapestry.annotations.Component;
028: import org.apache.tapestry.annotations.Persist;
029: import org.apache.tapestry.corelib.components.Form;
030: import org.apache.tapestry.ioc.internal.util.CollectionFactory;
031: import org.apache.tapestry.ioc.internal.util.InternalUtils;
032:
033: public class ClassLoaderInspect {
034: private static final ClassLoader _classLoader = Thread
035: .currentThread().getContextClassLoader();
036:
037: private ClassLoader _loader;
038:
039: @Persist
040: private String _resource;
041:
042: @Component
043: private Form _search;
044:
045: @Persist
046: private List<URL> _URLs;
047:
048: @Persist
049: private boolean _showMatches;
050:
051: private URL _URL;
052:
053: private JarEntry _jarEntry;
054:
055: public URL getURL() {
056: return _URL;
057: }
058:
059: public void setURL(URL url) {
060: _URL = url;
061: }
062:
063: public ClassLoader getClassLoader() {
064: return _classLoader;
065: }
066:
067: public ClassLoader getLoader() {
068: return _loader;
069: }
070:
071: public void setLoader(ClassLoader loader) {
072: _loader = loader;
073: }
074:
075: public List<ClassLoader> getLoaders() {
076: List<ClassLoader> result = CollectionFactory.newList();
077:
078: ClassLoader current = getClass().getClassLoader();
079:
080: while (current != null) {
081: result.add(0, current);
082:
083: current = current.getParent();
084: }
085:
086: return result;
087: }
088:
089: public int getListSize() {
090: return _URLs == null ? 0 : _URLs.size();
091: }
092:
093: void onFailure() {
094: _showMatches = false;
095: _URLs = null;
096: }
097:
098: void onSuccess() {
099: _showMatches = false;
100:
101: _URLs = null;
102:
103: try {
104: List<URL> urls = CollectionFactory.newList();
105:
106: Enumeration<URL> e = _classLoader.getResources(_resource);
107:
108: while (e.hasMoreElements())
109: urls.add(e.nextElement());
110:
111: _URLs = urls;
112:
113: _showMatches = true;
114: } catch (Exception ex) {
115: String message = ex.getMessage();
116:
117: if (InternalUtils.isBlank(message))
118: message = ex.getClass().getName();
119:
120: _search.recordError(message);
121: }
122: }
123:
124: public String getResource() {
125: return _resource;
126: }
127:
128: public void setResource(String resource) {
129: _resource = resource;
130: }
131:
132: public List<URL> getURLs() {
133: return _URLs;
134: }
135:
136: public boolean getShowMatches() {
137: return _showMatches;
138: }
139:
140: public String getContentStreamContents() {
141: StringBuilder builder = new StringBuilder();
142:
143: try {
144: InputStream is = _URL.openStream();
145: InputStreamReader reader = new InputStreamReader(is);
146:
147: char[] buffer = new char[1000];
148:
149: while (true) {
150: int length = reader.read(buffer);
151:
152: if (length < 0)
153: break;
154:
155: builder.append(buffer, 0, length);
156: }
157:
158: reader.close();
159:
160: return builder.toString();
161: } catch (Exception ex) {
162: return ex.getMessage();
163: }
164: }
165:
166: public List<JarEntry> getJarEntries() {
167: try {
168: URLConnection rawConnection = _URL.openConnection();
169:
170: JarURLConnection jarConnection = (JarURLConnection) rawConnection;
171:
172: JarEntry rootEntry = jarConnection.getJarEntry();
173:
174: List<JarEntry> result = CollectionFactory.newList();
175:
176: if (rootEntry.isDirectory()) {
177: Enumeration<JarEntry> e = jarConnection.getJarFile()
178: .entries();
179:
180: while (e.hasMoreElements())
181: result.add(e.nextElement());
182: } else {
183: result.add(rootEntry);
184: }
185:
186: return result;
187: } catch (Exception ex) {
188: return null;
189: }
190:
191: }
192:
193: public URLConnection getURLConnection() {
194: try {
195: return _URL.openConnection();
196: } catch (IOException ex) {
197: return null;
198: }
199: }
200:
201: public JarEntry getJarEntry() {
202: return _jarEntry;
203: }
204:
205: public void setJarEntry(JarEntry jarEntry) {
206: _jarEntry = jarEntry;
207: }
208: }
|