KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > explorer > plugin > java > reflect > JarFileContext


1 /*====================================================================
2
3 Objectweb Explorer Framework
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Jerome Moroy, Philippe Merle.
23 Contributor(s): ______________________________________.
24
25 ====================================================================*/

26
27 package org.objectweb.util.explorer.plugin.java.reflect;
28
29 import java.net.URL JavaDoc;
30 import java.net.URLClassLoader JavaDoc;
31 import java.util.Enumeration JavaDoc;
32 import java.util.Vector JavaDoc;
33 import java.util.jar.JarEntry JavaDoc;
34
35 import org.objectweb.util.explorer.api.Context;
36 import org.objectweb.util.explorer.api.Entry;
37 import org.objectweb.util.explorer.core.common.api.ContextContainer;
38 import org.objectweb.util.explorer.core.naming.lib.DefaultEntry;
39
40
41 /**
42  *
43  *
44  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>,
45  * <a HREF="mailto:Jerome.Moroy@lifl.fr">Jérôme Moroy</a>
46  *
47  * @version 0.1
48  */

49 public class JarFileContext
50     implements Context
51 {
52
53     //==================================================================
54
//
55
// Internal states.
56
//
57
//==================================================================
58

59     //==================================================================
60
//
61
// No constructor.
62
//
63
//==================================================================
64

65     //==================================================================
66
//
67
// Internal methods.
68
//
69
//==================================================================
70

71     /**
72      * Replace all the occurences of the given string with the given replacement.
73      * @param original The original String to modify.
74      * @param to_replace The substring of the string to search and replace.
75      * @param new_value The replacement String.
76      * @return The modified String.
77      */

78     protected String JavaDoc replaceAll(String JavaDoc original, String JavaDoc to_replace, String JavaDoc new_value) {
79         StringBuffer JavaDoc newString = new StringBuffer JavaDoc();
80         int indexBegin = 0;
81         int index = original.indexOf(to_replace, indexBegin);
82         while (index != -1) {
83             newString.append(original.substring(indexBegin, index));
84             newString.append(new_value);
85             indexBegin = index + to_replace.length();
86             index = original.indexOf(to_replace, indexBegin);
87         }
88         newString.append(original.substring(indexBegin));
89         return newString.toString();
90     }
91
92     protected ContextContainer getContext(ContextContainer contextContainer, String JavaDoc packageName){
93         if(packageName!=null && !packageName.equals("")) {
94             String JavaDoc currentPackage = null;
95             String JavaDoc otherPackage = null;
96             if(packageName.lastIndexOf(".")!=-1){
97                 currentPackage = packageName.substring(0,packageName.indexOf("."));
98                 otherPackage = packageName.substring(packageName.indexOf(".")+1);
99             } else {
100                 currentPackage = packageName;
101             }
102             if(currentPackage!=null) {
103                 Entry JavaDoc entry = contextContainer.getEntry(currentPackage);
104                 ContextContainer cc = (entry!=null)?(ContextContainer)entry.getValue():null;
105                 if(cc == null) {
106                     cc = new PackageContextContainer();
107                     contextContainer.addEntry(currentPackage, cc);
108                 }
109                 if(otherPackage!=null)
110                     return getContext(cc, otherPackage);
111                 else
112                     return cc;
113             } else
114                 return contextContainer;
115         }
116         return contextContainer;
117     }
118
119     protected void addClass(ContextContainer contextContainer, Class JavaDoc theClass) {
120         String JavaDoc packageName = theClass.getPackage().getName();
121         ContextContainer cc = getContext(contextContainer, packageName);
122         cc.addEntry(theClass.getName().substring(theClass.getName().lastIndexOf(".") + 1),theClass);
123     }
124
125     //==================================================================
126
//
127
// Public methods for Context interface.
128
//
129
//==================================================================
130

131     /* (non-Javadoc)
132      * @see org.objectweb.util.explorer.api.Context#getEntries(java.lang.Object)
133      */

134     public Entry JavaDoc[] getEntries(Object JavaDoc object){
135         JarFileStructure jarFile = (JarFileStructure)object;
136         URLClassLoader JavaDoc classLoader = new URLClassLoader JavaDoc(new URL JavaDoc[]{jarFile.getJarURL()});
137         ContextContainer cc = new PackageContextContainer();
138         Vector JavaDoc elements = new Vector JavaDoc();
139         Enumeration JavaDoc enumeration = jarFile.getJarFile().entries();
140         while(enumeration.hasMoreElements()) {
141             JarEntry JavaDoc entry = (JarEntry JavaDoc)enumeration.nextElement();
142             if(!entry.isDirectory()){
143                 try {
144                     Class JavaDoc c = Class.forName(replaceAll(entry.getName().substring(0, entry.getName().lastIndexOf('.')),"/","."),true,classLoader);
145                     addClass(cc, c);
146                     //elements.add(new DefaultEntry(c,new DefaultName(entry.getName()),this));
147
} catch (ClassNotFoundException JavaDoc e) {
148                     // Ignore
149
}
150             }
151         }
152         Entry JavaDoc[] entries = cc.getEntries(object);
153         for (int i = 0; i < entries.length; i++) {
154             elements.add(new DefaultEntry(entries[i].getName().toString(),entries[i].getValue()));
155         }
156         return (Entry JavaDoc[])elements.toArray(new Entry JavaDoc[0]);
157     }
158
159 }
160     
Popular Tags