KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > querying > impl > xtas > resource > plugins > ServletResource


1 package org.exoplatform.services.xml.querying.impl.xtas.resource.plugins;
2
3 import java.io.FileOutputStream JavaDoc;
4 import java.io.File JavaDoc;
5 import java.io.IOException JavaDoc;
6
7
8 import org.exoplatform.services.xml.querying.UniFormTransformationException;
9 import org.exoplatform.services.xml.querying.XMLWellFormedData;
10 import org.exoplatform.services.xml.querying.impl.xtas.WellFormedUniFormTree;
11 import org.exoplatform.services.xml.querying.impl.xtas.resource.Resource;
12 import org.xml.sax.InputSource JavaDoc;
13
14 import javax.servlet.ServletContext JavaDoc;
15
16 /**
17  * Represents XML resource mapped to a ServletContext's specified path as XTAS resource
18  * @version $Id: ServletResource.java 566 2005-01-25 12:50:49Z kravchuk $
19  */

20 public class ServletResource extends Resource {
21
22     private static String JavaDoc SERVLET_RESOURCE = "servlet:/";
23     private ServletContext JavaDoc context = null;
24
25
26     public ServletResource()
27     {
28     }
29
30     /**
31      * Inits resource named <code> resourceId </code>
32      */

33     public void init(String JavaDoc resourceId)
34     {
35        this.resourceId = resourceId.substring(SERVLET_RESOURCE.length());
36 // this.resourceId = resourceId;
37
}
38
39     public void setContext(Object JavaDoc context)
40     {
41 //System.out.println("Set Context:"+ context +" "+this);
42

43        if( !(context instanceof ServletContext JavaDoc) )
44          throw new ClassCastException JavaDoc("ServletResource.setContext():"+context.getClass().getName()+" not subclass of javax.servlet.ServletContext!");
45        this.context = (ServletContext JavaDoc) context;
46
47
48     }
49
50     /**
51      * Loads XML from the Servlet Resource
52      */

53     public XMLWellFormedData load() throws UniFormTransformationException, IOException JavaDoc
54     {
55
56 // URL path = context.getResource(resourceId);
57

58         try {
59
60             WellFormedUniFormTree tree = new WellFormedUniFormTree ();
61             tree.init ( new InputSource JavaDoc (getContext().getRealPath(resourceId)) );
62
63             return tree;
64
65         } catch (Exception JavaDoc e) {
66
67             throw new UniFormTransformationException("ServletResource: Can not create WellFormedUniFormTree (XML) Reason: " + e);
68         }
69     }
70
71     public void save(XMLWellFormedData tree) throws IOException JavaDoc
72     {
73
74
75         FileOutputStream JavaDoc fos = new FileOutputStream JavaDoc( getContext().getRealPath(resourceId) );
76
77         byte[] b = tree.getAsByteArray();
78
79         fos.write( b, 0, b.length );
80
81         fos.flush();
82         fos.close();
83
84     }
85
86     /**
87      * Creates resource by Id
88      */

89     public void create(XMLWellFormedData initTree) throws IOException JavaDoc
90     {
91
92 //System.out.println("Create: "+ context +" "+this);
93

94         File JavaDoc res = new File JavaDoc (getContext().getRealPath(resourceId));
95
96         if( !res.createNewFile() )
97             throw new IOException JavaDoc (" Resource '"+context.getResource(resourceId).toString()+"' already exists.");
98
99 // save( initTree );
100

101     }
102
103     /**
104      * drops resource
105      */

106     public void drop() throws IOException JavaDoc
107     {
108 // File res = new File (resourceId);
109

110         File JavaDoc res = new File JavaDoc (getContext().getRealPath(resourceId));
111
112         if( !res.delete() )
113             throw new IOException JavaDoc (" Resource '"+context.getResource(resourceId).toString()+"' can not be deleted.");
114
115 // throw new IOException (" File '"+res.getAbsolutePath()+"' can not be deleted.");
116
}
117
118     private ServletContext JavaDoc getContext() throws IOException JavaDoc
119     {
120        if( context == null )
121           throw new IOException JavaDoc("ServletContext in ServletResource '"+resourceId+"' IS NULL!");
122        return context;
123     }
124
125 }
126
Popular Tags