1 package org.codehaus.plexus.classworlds.launcher;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.File;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24 import java.util.ArrayList;
25 import java.util.Collections;
26 import java.util.Comparator;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30
31 import org.codehaus.plexus.classworlds.ClassWorld;
32 import org.codehaus.plexus.classworlds.realm.ClassRealm;
33 import org.codehaus.plexus.classworlds.realm.DuplicateRealmException;
34 import org.codehaus.plexus.classworlds.realm.NoSuchRealmException;
35
36
37
38
39
40
41
42 public class Configurator implements ConfigurationHandler
43 {
44
45
46
47 private Launcher launcher;
48
49 private ClassWorld world;
50
51
52
53
54 private Map<String, ClassRealm> configuredRealms;
55
56
57
58
59 private ClassRealm curRealm;
60
61 private ClassLoader foreignClassLoader = null;
62
63
64
65
66
67
68 public Configurator( Launcher launcher )
69 {
70 this.launcher = launcher;
71
72 configuredRealms = new HashMap<String, ClassRealm>();
73
74 if ( launcher != null )
75 {
76 this.foreignClassLoader = launcher.getSystemClassLoader();
77 }
78 }
79
80
81
82
83
84
85 public Configurator( ClassWorld world )
86 {
87 setClassWorld( world );
88 }
89
90
91
92
93
94
95
96 public void setClassWorld( ClassWorld world )
97 {
98 this.world = world;
99
100 configuredRealms = new HashMap<String, ClassRealm>();
101 }
102
103
104
105
106
107
108
109
110
111
112
113
114 public void configure( InputStream is )
115 throws IOException, ConfigurationException, DuplicateRealmException, NoSuchRealmException
116 {
117 if ( world == null )
118 {
119 world = new ClassWorld();
120 }
121
122 curRealm = null;
123
124 foreignClassLoader = null;
125
126 if ( this.launcher != null )
127 {
128 foreignClassLoader = this.launcher.getSystemClassLoader();
129 }
130
131 ConfigurationParser parser = new ConfigurationParser( this, System.getProperties() );
132
133 parser.parse( is );
134
135
136 associateRealms();
137
138 if ( this.launcher != null )
139 {
140 this.launcher.setWorld( world );
141 }
142
143 }
144
145
146
147
148
149 public void associateRealms()
150 {
151 List<String> sortRealmNames = new ArrayList<String>( configuredRealms.keySet() );
152
153
154 Comparator<String> comparator = new Comparator<String>()
155 {
156 public int compare( String g1,
157 String g2 )
158 {
159 return g1.compareTo( g2 );
160 }
161 };
162
163 Collections.sort( sortRealmNames, comparator );
164
165
166
167
168
169
170
171
172
173
174
175 for ( String realmName : sortRealmNames )
176 {
177 int j = realmName.lastIndexOf( '.' );
178
179 if ( j > 0 )
180 {
181 String parentRealmName = realmName.substring( 0, j );
182
183 ClassRealm parentRealm = configuredRealms.get( parentRealmName );
184
185 if ( parentRealm != null )
186 {
187 ClassRealm realm = configuredRealms.get( realmName );
188
189 realm.setParentRealm( parentRealm );
190 }
191 }
192 }
193 }
194
195 public void addImportFrom( String relamName, String importSpec )
196 throws NoSuchRealmException
197 {
198 curRealm.importFrom( relamName, importSpec );
199 }
200
201 public void addLoadFile( File file )
202 {
203 try
204 {
205 curRealm.addURL( file.toURI().toURL() );
206 }
207 catch ( MalformedURLException e )
208 {
209
210 }
211 }
212
213 public void addLoadURL( URL url )
214 {
215 curRealm.addURL( url );
216 }
217
218 public void addRealm( String realmName )
219 throws DuplicateRealmException
220 {
221 curRealm = world.newRealm( realmName, foreignClassLoader );
222
223
224 configuredRealms.put( realmName, curRealm );
225 }
226
227 public void setAppMain( String mainClassName, String mainRealmName )
228 {
229 if ( this.launcher != null )
230 {
231 this.launcher.setAppMain( mainClassName, mainRealmName );
232 }
233 }
234 }