KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > pm > ino > InoManager


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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 org.apache.ws.jaxme.pm.ino;
17
18 import java.io.StringWriter JavaDoc;
19 import java.io.UnsupportedEncodingException JavaDoc;
20 import java.lang.reflect.InvocationTargetException JavaDoc;
21
22 import javax.xml.bind.Element;
23 import javax.xml.bind.JAXBException;
24 import javax.xml.bind.Marshaller;
25 import javax.xml.parsers.SAXParserFactory JavaDoc;
26
27 import org.xml.sax.SAXException JavaDoc;
28 import org.apache.ws.jaxme.JMManager;
29 import org.apache.ws.jaxme.JMUnmarshallerHandler;
30 import org.apache.ws.jaxme.Observer;
31 import org.apache.ws.jaxme.PMException;
32 import org.apache.ws.jaxme.PMParams;
33 import org.apache.ws.jaxme.pm.impl.PMIdImpl;
34
35
36
37 /** <p>An implementation of a JMManager for a Tamino database.</p>
38  *
39  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>s
40  */

41 public class InoManager extends PMIdImpl {
42   private java.net.URL JavaDoc dbURL;
43   private String JavaDoc user, password;
44   private String JavaDoc idTag, elementTag;
45   private boolean useGet;
46   private static final javax.xml.parsers.SAXParserFactory JavaDoc spf;
47   private static final IURLEncoder encoder;
48
49   static {
50     spf = SAXParserFactory.newInstance();
51     spf.setNamespaceAware(true);
52     spf.setValidating(false);
53     IURLEncoder e;
54     try {
55         String JavaDoc myClassName = InoManager.class.getName();
56         int offset = myClassName.lastIndexOf('.');
57         String JavaDoc packageName = myClassName.substring(0, offset);
58         e = (IURLEncoder) Class.forName(packageName + ".URLEncoder14").newInstance();
59     } catch (Throwable JavaDoc t) {
60         e = new URLEncoder();
61     }
62     encoder = e;
63   }
64
65   protected String JavaDoc getUser() {
66       return user;
67   }
68
69   protected String JavaDoc getPassword() {
70       return password;
71   }
72
73   /** Returns the qualified element name of the root element.
74    * This is used in delete or select queries for creation
75    * of an XPath query.
76    */

77   public String JavaDoc getElementTag() {
78     return elementTag;
79   }
80
81   /** Sets the qualified element name of the root element.
82    * This is used in delete or select queries for creation
83    * of an XQL statement.
84    */

85   public void setElementTag(String JavaDoc pElementTag) {
86     elementTag = pElementTag;
87   }
88
89   /** Returns the qualified attribute name of the ID attribute.
90    * This is used in delete or update queries for creation
91    * of an XQL statement.
92    */

93   public String JavaDoc getIdTag() {
94     return idTag;
95   }
96
97   /** Returns the qualified attribute name of the ID attribute.
98    * This is used in delete or update queries for creation
99    * of an XQL statement.
100    */

101   public void setIdTag(String JavaDoc pIdTag) {
102     idTag = pIdTag;
103   }
104
105   public void init(JMManager pManager) throws JAXBException {
106     super.init(pManager);
107     idTag = pManager.getProperty("ino.idTag");
108     if (idTag == null || idTag.length() == 0) {
109       throw new JAXBException("Missing property: 'ino.idTag' (Tag name or attribute name of the element ID)");
110     }
111     elementTag = pManager.getProperty("ino.elementTag");
112     if (elementTag == null || elementTag.length() == 0) {
113       throw new JAXBException("Missing property: 'ino.elementTag' (Qualified element name, including namespace prefix)");
114     }
115     String JavaDoc url = pManager.getProperty("ino.url");
116     if (url == null || url.length() == 0) {
117       throw new JAXBException("Missing property: 'ino.url' (Tamino database URL)");
118     }
119     user = pManager.getProperty("ino.user");
120     password = pManager.getProperty("ino.password");
121     useGet = Boolean.valueOf(pManager.getProperty("ino.useGet")).booleanValue();
122   }
123
124   /** <p>Returns a query suited for deleting the element.</p>
125    */

126   protected String JavaDoc getDeleteQuery(Element pElement)
127       throws JAXBException, InvocationTargetException JavaDoc, IllegalAccessException JavaDoc,
128              NoSuchMethodException JavaDoc, UnsupportedEncodingException JavaDoc {
129     String JavaDoc id = getId(pElement);
130     if (id == null || id.length() == 0) {
131       throw new JAXBException("The element being deleted doesn't have an ID.");
132     }
133     return "_delete=" +
134       encoder.encode(getElementTag() + '[' + getIdTag() + '=' + id + ']');
135   }
136
137   /** <p>Returns a query suited for updating the element.</p>
138    */

139   protected String JavaDoc getUpdateQuery(Element pElement)
140         throws JAXBException, UnsupportedEncodingException JavaDoc {
141     StringWriter JavaDoc sw = new StringWriter JavaDoc();
142     Marshaller marshaller = getManager().getFactory().createMarshaller();
143     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
144     marshaller.marshal(pElement, sw);
145     return "_process=" + encoder.encode(sw.toString());
146   }
147
148   /** <p>Returns a query suited for inserting the element.</p>
149    */

150   protected String JavaDoc getInsertQuery(Element pElement)
151         throws JAXBException, UnsupportedEncodingException JavaDoc {
152     StringWriter JavaDoc sw = new StringWriter JavaDoc();
153     Marshaller marshaller = getManager().getFactory().createMarshaller();
154     marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
155     marshaller.marshal(pElement, sw);
156     return "_process=" + encoder.encode(sw.toString());
157   }
158
159   /** <p>Performs a single database query.</p>
160    */

161   protected java.net.HttpURLConnection JavaDoc getResponse(String JavaDoc pQuery) throws SAXException JavaDoc {
162     java.net.URL JavaDoc connectionURL = dbURL;
163     try {
164       if (useGet) {
165         String JavaDoc dburl = connectionURL.toString();
166         String JavaDoc url;
167         if (dburl.indexOf('?') > 0) {
168           url = dburl + "&" + pQuery;
169         } else {
170           url = dburl + "?" + pQuery;
171         }
172         try {
173           connectionURL = new java.net.URL JavaDoc(url);
174           java.net.HttpURLConnection JavaDoc conn =
175             (java.net.HttpURLConnection JavaDoc) connectionURL.openConnection();
176           conn.setDoOutput(false);
177           conn.setDoInput(true);
178           return conn;
179         } catch (java.net.MalformedURLException JavaDoc e) {
180           throw new SAXException JavaDoc("Malformed database URL: " + url);
181         }
182       } else {
183         java.net.HttpURLConnection JavaDoc conn =
184           (java.net.HttpURLConnection JavaDoc) connectionURL.openConnection();
185         conn.setDoOutput(true);
186         conn.setDoInput(true);
187
188         java.io.OutputStream JavaDoc ostream = conn.getOutputStream();
189         java.io.Writer JavaDoc w = new java.io.OutputStreamWriter JavaDoc(ostream);
190         w.write(pQuery);
191         w.close();
192         return conn;
193       }
194     } catch (java.io.IOException JavaDoc e) {
195       throw new SAXException JavaDoc("I/O Error: " + e.getMessage(), e);
196     }
197   }
198
199   /** <p>Performs a single database query.</p>
200    */

201   protected InoResponseHandler performQuery(String JavaDoc pQuery, java.util.List JavaDoc pList)
202       throws SAXException JavaDoc {
203     InoResponseHandler irh = new InoResponseHandler();
204     if (pList != null) {
205       irh.setInoObjectIdList(pList);
206     }
207     performQuery(pQuery, irh);
208     return irh;
209   }
210
211   /** <p>Parses a single INO response document.</p>
212    */

213   protected void performQuery(String JavaDoc pQuery, InoResponseHandler pHandler)
214       throws SAXException JavaDoc {
215     java.net.HttpURLConnection JavaDoc conn = getResponse(pQuery);
216     try {
217       org.xml.sax.InputSource JavaDoc isource = new org.xml.sax.InputSource JavaDoc(conn.getInputStream());
218       isource.setEncoding(conn.getContentEncoding());
219       org.xml.sax.XMLReader JavaDoc xr;
220       javax.xml.parsers.SAXParser JavaDoc sp = spf.newSAXParser();
221       xr = sp.getXMLReader();
222       xr.setContentHandler(pHandler);
223       xr.parse(isource);
224     } catch (javax.xml.parsers.ParserConfigurationException JavaDoc e) {
225       throw new SAXException JavaDoc("ParserConfigurationException: " + e.getMessage(), e);
226     } catch (java.io.IOException JavaDoc e) {
227       throw new SAXException JavaDoc("I/O Exception: " + e.getMessage(), e);
228     }
229   }
230
231   /* (non-Javadoc)
232    * @see org.apache.ws.jaxme.PM#select(org.apache.ws.jaxme.Observer, java.lang.String, org.apache.ws.jaxme.PMParams)
233    */

234   public void select(Observer pObserver, String JavaDoc pQuery, PMParams pPlaceHolderArgs) throws JAXBException {
235     pQuery = super.parseQuery(pQuery, pPlaceHolderArgs);
236     String JavaDoc q;
237     int max = pPlaceHolderArgs.getMaxResultDocuments();
238     int skip = pPlaceHolderArgs.getSkippedResultDocuments();
239     if (max != 0 || skip != 0) {
240       q = "_xql(" + (skip+1) + "," + max + ")=";
241     } else {
242       q = "_xql=";
243     }
244
245     InoResponseHandler irh = new InoResponseHandler();
246     try {
247         q += encoder.encode(pQuery);
248     } catch (UnsupportedEncodingException JavaDoc e) {
249         throw new PMException(e);
250     }
251     JMUnmarshallerHandler handler = (JMUnmarshallerHandler) getManager().getFactory().createUnmarshaller().getUnmarshallerHandler();
252     handler.setObserver(pObserver);
253     irh.setResultHandler(handler);
254     try {
255         performQuery(q, irh);
256     } catch (SAXException JavaDoc e) {
257         throw new PMException(e);
258     }
259   }
260
261   /* (non-Javadoc)
262    * @see org.apache.ws.jaxme.PM#insert(javax.xml.bind.Element)
263    */

264   public void insert(javax.xml.bind.Element pElement) throws PMException {
265     try {
266       String JavaDoc query = getInsertQuery(pElement);
267       java.util.List JavaDoc idList = new java.util.ArrayList JavaDoc();
268       performQuery(query, idList);
269       if (idList.size() == 0) {
270         throw new PMException("Query did not return an ino:id");
271       }
272     } catch (SAXException JavaDoc e) {
273       throw new PMException(e);
274     } catch (JAXBException e) {
275       if (e instanceof PMException) {
276         throw (PMException) e;
277       } else {
278         throw new PMException(e);
279       }
280     } catch (UnsupportedEncodingException JavaDoc e) {
281         throw new PMException(e);
282     }
283   }
284
285   /* (non-Javadoc)
286    * @see org.apache.ws.jaxme.PM#update(javax.xml.bind.Element)
287    */

288   public void update(javax.xml.bind.Element pElement) throws PMException {
289       try {
290           String JavaDoc query = getUpdateQuery(pElement);
291           performQuery(query, (java.util.List JavaDoc) null);
292       } catch (SAXException JavaDoc e) {
293           throw new PMException(e);
294       } catch (UnsupportedEncodingException JavaDoc e) {
295           throw new PMException(e);
296       } catch (JAXBException e) {
297           if (e instanceof PMException) {
298               throw (PMException) e;
299           } else {
300               throw new PMException(e);
301           }
302       }
303   }
304
305   /* (non-Javadoc)
306    * @see org.apache.ws.jaxme.PM#delete(javax.xml.bind.Element)
307    */

308   public void delete(javax.xml.bind.Element pElement) throws PMException {
309     try {
310       String JavaDoc query = getDeleteQuery(pElement);
311       performQuery(query, (java.util.List JavaDoc) null);
312     } catch (NoSuchMethodException JavaDoc e) {
313       throw new PMException(e);
314     } catch (IllegalAccessException JavaDoc e) {
315       throw new PMException(e);
316     } catch (InvocationTargetException JavaDoc e) {
317       throw new PMException(e.getTargetException());
318     } catch (SAXException JavaDoc e) {
319       throw new PMException(e);
320     } catch (JAXBException e) {
321       if (e instanceof PMException) {
322         throw (PMException) e;
323       } else {
324         throw new PMException(e);
325       }
326     } catch (UnsupportedEncodingException JavaDoc e) {
327         throw new PMException(e);
328     }
329   }
330 }
331
Popular Tags