01: /*
02: * Copyright 1999-2004 The Apache Software Foundation.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package com.opensymphony.webwork.util.classloader.stores;
17:
18: import org.apache.commons.io.IOUtils;
19:
20: import java.io.*;
21:
22: /**
23: * @author tcurdt
24: */
25: public final class FileResourceStore implements ResourceStore {
26:
27: private final File root;
28:
29: public FileResourceStore(final File pFile) {
30: root = pFile;
31: }
32:
33: public byte[] read(final String resourceName) {
34: InputStream is = null;
35: try {
36: is = new FileInputStream(getFile(resourceName));
37: final byte[] data = IOUtils.toByteArray(is);
38: return data;
39: } catch (FileNotFoundException e) {
40: } catch (IOException e) {
41: } finally {
42: if (is != null) {
43: try {
44: is.close();
45: } catch (IOException e) {
46: }
47: }
48: }
49:
50: return null;
51: }
52:
53: public void write(final String resourceName, final byte[] clazzData) {
54: OutputStream os = null;
55: try {
56: final File file = getFile(resourceName);
57: final File parent = file.getParentFile();
58: if (!parent.exists()) {
59: if (!parent.mkdirs()) {
60: throw new IOException("could not create" + parent);
61: }
62: }
63: os = new FileOutputStream(file);
64: os.write(clazzData);
65: } catch (FileNotFoundException e) {
66: } catch (IOException e) {
67: } finally {
68: if (os != null) {
69: try {
70: os.close();
71: } catch (IOException e) {
72: }
73: }
74: }
75: }
76:
77: public void remove(final String pResourceName) {
78: getFile(pResourceName).delete();
79: }
80:
81: private File getFile(final String pResourceName) {
82: final String fileName = pResourceName.replace('.',
83: File.separatorChar)
84: + ".class";
85: return new File(root, fileName);
86: }
87:
88: }
|