1 16 package org.apache.ws.jaxme.pm.ino; 17 18 import java.io.StringWriter ; 19 import java.io.UnsupportedEncodingException ; 20 import java.lang.reflect.InvocationTargetException ; 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 ; 26 27 import org.xml.sax.SAXException ; 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 41 public class InoManager extends PMIdImpl { 42 private java.net.URL dbURL; 43 private String user, password; 44 private String idTag, elementTag; 45 private boolean useGet; 46 private static final javax.xml.parsers.SAXParserFactory 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 myClassName = InoManager.class.getName(); 56 int offset = myClassName.lastIndexOf('.'); 57 String packageName = myClassName.substring(0, offset); 58 e = (IURLEncoder) Class.forName(packageName + ".URLEncoder14").newInstance(); 59 } catch (Throwable t) { 60 e = new URLEncoder(); 61 } 62 encoder = e; 63 } 64 65 protected String getUser() { 66 return user; 67 } 68 69 protected String getPassword() { 70 return password; 71 } 72 73 77 public String getElementTag() { 78 return elementTag; 79 } 80 81 85 public void setElementTag(String pElementTag) { 86 elementTag = pElementTag; 87 } 88 89 93 public String getIdTag() { 94 return idTag; 95 } 96 97 101 public void setIdTag(String 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 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 126 protected String getDeleteQuery(Element pElement) 127 throws JAXBException, InvocationTargetException , IllegalAccessException , 128 NoSuchMethodException , UnsupportedEncodingException { 129 String 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 139 protected String getUpdateQuery(Element pElement) 140 throws JAXBException, UnsupportedEncodingException { 141 StringWriter sw = new StringWriter (); 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 150 protected String getInsertQuery(Element pElement) 151 throws JAXBException, UnsupportedEncodingException { 152 StringWriter sw = new StringWriter (); 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 161 protected java.net.HttpURLConnection getResponse(String pQuery) throws SAXException { 162 java.net.URL connectionURL = dbURL; 163 try { 164 if (useGet) { 165 String dburl = connectionURL.toString(); 166 String 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 (url); 174 java.net.HttpURLConnection conn = 175 (java.net.HttpURLConnection ) connectionURL.openConnection(); 176 conn.setDoOutput(false); 177 conn.setDoInput(true); 178 return conn; 179 } catch (java.net.MalformedURLException e) { 180 throw new SAXException ("Malformed database URL: " + url); 181 } 182 } else { 183 java.net.HttpURLConnection conn = 184 (java.net.HttpURLConnection ) connectionURL.openConnection(); 185 conn.setDoOutput(true); 186 conn.setDoInput(true); 187 188 java.io.OutputStream ostream = conn.getOutputStream(); 189 java.io.Writer w = new java.io.OutputStreamWriter (ostream); 190 w.write(pQuery); 191 w.close(); 192 return conn; 193 } 194 } catch (java.io.IOException e) { 195 throw new SAXException ("I/O Error: " + e.getMessage(), e); 196 } 197 } 198 199 201 protected InoResponseHandler performQuery(String pQuery, java.util.List pList) 202 throws SAXException { 203 InoResponseHandler irh = new InoResponseHandler(); 204 if (pList != null) { 205 irh.setInoObjectIdList(pList); 206 } 207 performQuery(pQuery, irh); 208 return irh; 209 } 210 211 213 protected void performQuery(String pQuery, InoResponseHandler pHandler) 214 throws SAXException { 215 java.net.HttpURLConnection conn = getResponse(pQuery); 216 try { 217 org.xml.sax.InputSource isource = new org.xml.sax.InputSource (conn.getInputStream()); 218 isource.setEncoding(conn.getContentEncoding()); 219 org.xml.sax.XMLReader xr; 220 javax.xml.parsers.SAXParser sp = spf.newSAXParser(); 221 xr = sp.getXMLReader(); 222 xr.setContentHandler(pHandler); 223 xr.parse(isource); 224 } catch (javax.xml.parsers.ParserConfigurationException e) { 225 throw new SAXException ("ParserConfigurationException: " + e.getMessage(), e); 226 } catch (java.io.IOException e) { 227 throw new SAXException ("I/O Exception: " + e.getMessage(), e); 228 } 229 } 230 231 234 public void select(Observer pObserver, String pQuery, PMParams pPlaceHolderArgs) throws JAXBException { 235 pQuery = super.parseQuery(pQuery, pPlaceHolderArgs); 236 String 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 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 e) { 257 throw new PMException(e); 258 } 259 } 260 261 264 public void insert(javax.xml.bind.Element pElement) throws PMException { 265 try { 266 String query = getInsertQuery(pElement); 267 java.util.List idList = new java.util.ArrayList (); 268 performQuery(query, idList); 269 if (idList.size() == 0) { 270 throw new PMException("Query did not return an ino:id"); 271 } 272 } catch (SAXException 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 e) { 281 throw new PMException(e); 282 } 283 } 284 285 288 public void update(javax.xml.bind.Element pElement) throws PMException { 289 try { 290 String query = getUpdateQuery(pElement); 291 performQuery(query, (java.util.List ) null); 292 } catch (SAXException e) { 293 throw new PMException(e); 294 } catch (UnsupportedEncodingException 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 308 public void delete(javax.xml.bind.Element pElement) throws PMException { 309 try { 310 String query = getDeleteQuery(pElement); 311 performQuery(query, (java.util.List ) null); 312 } catch (NoSuchMethodException e) { 313 throw new PMException(e); 314 } catch (IllegalAccessException e) { 315 throw new PMException(e); 316 } catch (InvocationTargetException e) { 317 throw new PMException(e.getTargetException()); 318 } catch (SAXException 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 e) { 327 throw new PMException(e); 328 } 329 } 330 } 331 | Popular Tags |