24
24
* SOFTWARE.
25
25
*/
26
26
27
+ import javax .inject .Inject ;
28
+ import javax .inject .Named ;
29
+
30
+ import java .io .File ;
31
+ import java .io .FileOutputStream ;
32
+ import java .io .IOException ;
33
+ import java .io .InputStream ;
34
+ import java .io .OutputStream ;
35
+ import java .util .Map ;
27
36
28
37
import org .codehaus .plexus .resource .loader .FileResourceCreationException ;
29
38
import org .codehaus .plexus .resource .loader .ResourceIOException ;
34
43
import org .slf4j .Logger ;
35
44
import org .slf4j .LoggerFactory ;
36
45
37
- import java .io .File ;
38
- import java .io .FileOutputStream ;
39
- import java .io .IOException ;
40
- import java .io .InputStream ;
41
- import java .io .OutputStream ;
42
- import java .util .Map ;
43
- import javax .inject .Inject ;
44
- import javax .inject .Named ;
45
-
46
46
/**
47
47
* @author <a href="mailto:trygvis@inamo.no">Trygve Laugstøl</a>
48
48
* @author Jason van Zyl
49
49
* @version $Id$
50
50
*/
51
51
@ Named
52
- public class DefaultResourceManager implements ResourceManager
53
- {
54
- private static final Logger LOGGER = LoggerFactory .getLogger ( DefaultResourceManager .class );
52
+ public class DefaultResourceManager implements ResourceManager {
53
+ private static final Logger LOGGER = LoggerFactory .getLogger (DefaultResourceManager .class );
55
54
56
55
private final Map <String , ResourceLoader > resourceLoaders ;
57
56
58
57
private File outputDirectory ;
59
58
60
59
@ Inject
61
- public DefaultResourceManager ( Map <String , ResourceLoader > resourceLoaders )
62
- {
60
+ public DefaultResourceManager (Map <String , ResourceLoader > resourceLoaders ) {
63
61
this .resourceLoaders = resourceLoaders ;
64
62
}
65
63
@@ -68,173 +66,131 @@ public DefaultResourceManager( Map<String, ResourceLoader> resourceLoaders )
68
66
// ----------------------------------------------------------------------
69
67
70
68
@ Override
71
- public InputStream getResourceAsInputStream ( String name )
72
- throws ResourceNotFoundException
73
- {
74
- PlexusResource resource = getResource ( name );
75
- try
76
- {
69
+ public InputStream getResourceAsInputStream (String name ) throws ResourceNotFoundException {
70
+ PlexusResource resource = getResource (name );
71
+ try {
77
72
return resource .getInputStream ();
78
- }
79
- catch ( IOException e )
80
- {
81
- throw new ResourceIOException ( "Failed to open resource " + resource .getName () + ": " + e .getMessage (), e );
73
+ } catch (IOException e ) {
74
+ throw new ResourceIOException ("Failed to open resource " + resource .getName () + ": " + e .getMessage (), e );
82
75
}
83
76
}
84
77
85
78
@ Override
86
- public File getResourceAsFile ( String name )
87
- throws ResourceNotFoundException , FileResourceCreationException
88
- {
89
- return getResourceAsFile ( getResource ( name ) );
79
+ public File getResourceAsFile (String name ) throws ResourceNotFoundException , FileResourceCreationException {
80
+ return getResourceAsFile (getResource (name ));
90
81
}
91
82
92
83
@ Override
93
- public File getResourceAsFile ( String name , String outputPath )
94
- throws ResourceNotFoundException , FileResourceCreationException
95
- {
96
- if ( outputPath == null )
97
- {
98
- return getResourceAsFile ( name );
84
+ public File getResourceAsFile (String name , String outputPath )
85
+ throws ResourceNotFoundException , FileResourceCreationException {
86
+ if (outputPath == null ) {
87
+ return getResourceAsFile (name );
99
88
}
100
- PlexusResource resource = getResource ( name );
89
+ PlexusResource resource = getResource (name );
101
90
File outputFile ;
102
- if ( outputDirectory != null )
103
- {
104
- outputFile = new File ( outputDirectory , outputPath );
105
- }
106
- else
107
- {
108
- outputFile = new File ( outputPath );
91
+ if (outputDirectory != null ) {
92
+ outputFile = new File (outputDirectory , outputPath );
93
+ } else {
94
+ outputFile = new File (outputPath );
109
95
}
110
- createResourceAsFile ( resource , outputFile );
96
+ createResourceAsFile (resource , outputFile );
111
97
return outputFile ;
112
98
}
113
99
114
100
@ Override
115
- public File resolveLocation ( String name , String outputPath )
116
- {
101
+ public File resolveLocation (String name , String outputPath ) {
117
102
// Honour what the original locator does and return null ...
118
- try
119
- {
120
- return getResourceAsFile ( name , outputPath );
121
- }
122
- catch ( Exception e )
123
- {
103
+ try {
104
+ return getResourceAsFile (name , outputPath );
105
+ } catch (Exception e ) {
124
106
return null ;
125
107
}
126
108
}
127
109
128
110
@ Override
129
- public File resolveLocation ( String name )
130
- {
111
+ public File resolveLocation (String name ) {
131
112
// Honour what the original locator does and return null ...
132
- try
133
- {
134
- return getResourceAsFile ( name );
135
- }
136
- catch ( Exception e )
137
- {
113
+ try {
114
+ return getResourceAsFile (name );
115
+ } catch (Exception e ) {
138
116
return null ;
139
117
}
140
118
}
141
119
142
120
@ Override
143
- public void setOutputDirectory ( File outputDirectory )
144
- {
121
+ public void setOutputDirectory (File outputDirectory ) {
145
122
this .outputDirectory = outputDirectory ;
146
123
}
147
124
148
125
@ Override
149
- public void addSearchPath ( String id , String path )
150
- {
151
- ResourceLoader loader = resourceLoaders .get ( id );
126
+ public void addSearchPath (String id , String path ) {
127
+ ResourceLoader loader = resourceLoaders .get (id );
152
128
153
- if ( loader == null )
154
- {
155
- throw new IllegalArgumentException ( "unknown resource loader: " + id );
129
+ if (loader == null ) {
130
+ throw new IllegalArgumentException ("unknown resource loader: " + id );
156
131
}
157
132
158
- loader .addSearchPath ( path );
133
+ loader .addSearchPath (path );
159
134
}
160
135
161
136
@ Override
162
- public PlexusResource getResource ( String name )
163
- throws ResourceNotFoundException
164
- {
165
- for ( ResourceLoader resourceLoader : resourceLoaders .values () )
166
- {
167
- try
168
- {
169
- PlexusResource resource = resourceLoader .getResource ( name );
137
+ public PlexusResource getResource (String name ) throws ResourceNotFoundException {
138
+ for (ResourceLoader resourceLoader : resourceLoaders .values ()) {
139
+ try {
140
+ PlexusResource resource = resourceLoader .getResource (name );
170
141
171
- LOGGER .debug ( "The resource '{}' was found as '{}'" , name , resource .getName () );
142
+ LOGGER .debug ("The resource '{}' was found as '{}'" , name , resource .getName ());
172
143
173
144
return resource ;
174
- }
175
- catch ( ResourceNotFoundException e )
176
- {
177
- LOGGER . debug ( "The resource '{}' was not found with resourceLoader '{}'" ,
178
- name , resourceLoader .getClass ().getName () );
145
+ } catch ( ResourceNotFoundException e ) {
146
+ LOGGER . debug (
147
+ "The resource '{}' was not found with resourceLoader '{}'" ,
148
+ name ,
149
+ resourceLoader .getClass ().getName ());
179
150
}
180
151
}
181
152
182
- throw new ResourceNotFoundException ( name );
153
+ throw new ResourceNotFoundException (name );
183
154
}
184
155
185
156
@ Override
186
- public File getResourceAsFile ( PlexusResource resource )
187
- throws FileResourceCreationException
188
- {
189
- try
190
- {
157
+ public File getResourceAsFile (PlexusResource resource ) throws FileResourceCreationException {
158
+ try {
191
159
File f = resource .getFile ();
192
- if ( f != null )
193
- {
160
+ if (f != null ) {
194
161
return f ;
195
162
}
196
- }
197
- catch ( IOException e )
198
- {
163
+ } catch (IOException e ) {
199
164
// Ignore this, try to make use of resource.getInputStream().
200
165
}
201
166
202
- final File outputFile = FileUtils .createTempFile ( "plexus-resources" , "tmp" , outputDirectory );
167
+ final File outputFile = FileUtils .createTempFile ("plexus-resources" , "tmp" , outputDirectory );
203
168
outputFile .deleteOnExit ();
204
- createResourceAsFile ( resource , outputFile );
169
+ createResourceAsFile (resource , outputFile );
205
170
return outputFile ;
206
171
}
207
172
208
173
@ Override
209
- public void createResourceAsFile ( PlexusResource resource , File outputFile )
210
- throws FileResourceCreationException
211
- {
174
+ public void createResourceAsFile (PlexusResource resource , File outputFile ) throws FileResourceCreationException {
212
175
InputStream is = null ;
213
176
OutputStream os = null ;
214
- try
215
- {
177
+ try {
216
178
is = resource .getInputStream ();
217
179
File dir = outputFile .getParentFile ();
218
- if ( !dir .isDirectory () && !dir .mkdirs () )
219
- {
220
- throw new FileResourceCreationException ( "Failed to create directory " + dir .getPath () );
180
+ if (!dir .isDirectory () && !dir .mkdirs ()) {
181
+ throw new FileResourceCreationException ("Failed to create directory " + dir .getPath ());
221
182
}
222
- os = new FileOutputStream ( outputFile );
223
- IOUtil .copy ( is , os );
183
+ os = new FileOutputStream (outputFile );
184
+ IOUtil .copy (is , os );
224
185
is .close ();
225
186
is = null ;
226
187
os .close ();
227
188
os = null ;
228
- }
229
- catch ( IOException e )
230
- {
231
- throw new FileResourceCreationException ( "Cannot create file-based resource:" + e .getMessage (), e );
232
- }
233
- finally
234
- {
235
- IOUtil .close ( is );
236
- IOUtil .close ( os );
189
+ } catch (IOException e ) {
190
+ throw new FileResourceCreationException ("Cannot create file-based resource:" + e .getMessage (), e );
191
+ } finally {
192
+ IOUtil .close (is );
193
+ IOUtil .close (os );
237
194
}
238
195
}
239
-
240
196
}
0 commit comments