1 16 17 package org.apache.ws.jaxme.pm.ino.api4j; 18 19 import javax.naming.InitialContext ; 20 import javax.naming.NamingException ; 21 import javax.xml.bind.Element; 22 import javax.xml.bind.JAXBException; 23 24 import org.apache.ws.jaxme.JMManager; 25 import org.apache.ws.jaxme.Observer; 26 import org.apache.ws.jaxme.PMException; 27 import org.apache.ws.jaxme.PMParams; 28 import org.apache.ws.jaxme.impl.JAXBContextImpl; 29 import org.apache.ws.jaxme.impl.JMUnmarshallerHandlerImpl; 30 import org.apache.ws.jaxme.pm.impl.PMImpl; 31 import org.apache.ws.jaxme.pm.ino.InoObject; 32 33 import com.softwareag.tamino.db.api.accessor.TAccessLocation; 34 import com.softwareag.tamino.db.api.accessor.TDeleteException; 35 import com.softwareag.tamino.db.api.accessor.TInsertException; 36 import com.softwareag.tamino.db.api.accessor.TQuery; 37 import com.softwareag.tamino.db.api.accessor.TQueryException; 38 import com.softwareag.tamino.db.api.accessor.TUpdateException; 39 import com.softwareag.tamino.db.api.accessor.TXMLObjectAccessor; 40 import com.softwareag.tamino.db.api.connection.TConnection; 41 import com.softwareag.tamino.db.api.connection.TServerNotAvailableException; 42 import com.softwareag.tamino.db.api.connector.TaminoDataSource; 43 import com.softwareag.tamino.db.api.objectModel.TIteratorException; 44 import com.softwareag.tamino.db.api.objectModel.TNoSuchXMLObjectException; 45 import com.softwareag.tamino.db.api.objectModel.TXMLObject; 46 import com.softwareag.tamino.db.api.objectModel.TXMLObjectIterator; 47 import com.softwareag.tamino.db.api.objectModel.TXMLObjectModel; 48 import com.softwareag.tamino.db.api.objectModel.sax.TSAXObjectModel; 49 import com.softwareag.tamino.db.api.response.TResponse; 50 51 54 public class TaminoAPI4JPm extends PMImpl { 55 private static final ThreadLocal context = new ThreadLocal (); 56 private static TSAXObjectModel model; 57 private static boolean isInitialized; 58 59 static JAXBContextImpl getJAXBContext() { 60 return (JAXBContextImpl) context.get(); 61 } 62 63 static void setJAXBContext(JAXBContextImpl pContext) { 64 context.set(pContext); 65 } 66 67 private String collection; 68 private String jndiReference; 69 70 public String getCollection() { 71 return collection; 72 } 73 74 public void setCollection(String pCollection) { 75 collection = pCollection; 76 } 77 78 public String getJndiReference() { 79 return jndiReference; 80 } 81 82 public void setJndiReference(String pJndiReference) { 83 jndiReference = pJndiReference; 84 } 85 86 public void init(JMManager pManager) throws JAXBException { 87 super.init(pManager); 88 if (!isInitialized) { 89 synchronized(TaminoAPI4JPm.class) { 90 if (!isInitialized) { 91 System.err.println("Registering object model in class " + TXMLObjectModel.class + " with class loader " + TXMLObjectModel.class.getClassLoader()); 92 TSAXObjectModel m = new TSAXObjectModel(TaminoAPI4JPm.class.getName(), 93 TJMElement.class, TJMElement.class, 94 new DocumentDefaultHandler(), 95 new ElementDefaultHandler()); 96 TXMLObjectModel.register(m); 97 model = m; 98 isInitialized = true; 99 } 100 } 101 } 102 collection = pManager.getProperty("collection"); 103 if (collection == null || collection.length() == 0) { 104 throw new JAXBException("The property 'collection' must be set."); 105 } 106 jndiReference = pManager.getProperty("jndiReference"); 107 if (jndiReference == null || jndiReference.length() == 0) { 108 throw new JAXBException("The property 'jndiReference' must be set."); 109 } 110 } 111 112 protected TXMLObject getTXMLObject(InoObject pElement) { 113 return TXMLObject.newInstance(new TJMElement(pElement)); 114 } 115 116 protected TSAXObjectModel getTSAXObjectModel() throws JAXBException { 117 return model; 118 } 119 120 protected TConnection getTConnection() throws NamingException , TServerNotAvailableException { 121 InitialContext ic = new InitialContext (); 122 TaminoDataSource tds = (TaminoDataSource) ic.lookup(jndiReference); 123 TConnection con = tds.getConnection(); 124 return con; 125 } 126 127 public void select(Observer pObserver, String pQuery, PMParams pParams) throws PMException { 128 TConnection conn = null; 129 try { 130 JAXBContextImpl c = getManager().getFactory(); 131 setJAXBContext(c); 132 JMUnmarshallerHandlerImpl h = (JMUnmarshallerHandlerImpl) c.createUnmarshaller().getUnmarshallerHandler(); 133 DocumentDefaultHandler.setUnmarshallerHandler(h); 134 ElementDefaultHandler.initData(); 135 conn = getTConnection(); 136 TXMLObjectAccessor accessor = conn.newXMLObjectAccessor(TAccessLocation.newInstance(collection), model); 137 TResponse response = accessor.query(TQuery.newInstance(pQuery)); 138 for (TXMLObjectIterator iter = response.getXMLObjectIterator(); 139 iter.hasNext(); ) { 140 TXMLObject object = iter.next(); 141 pObserver.notify(((TJMElement) object.getElement()).getJMElement()); 142 } 143 } catch (TServerNotAvailableException e) { 144 throw new PMException(e); 145 } catch (NamingException e) { 146 throw new PMException(e); 147 } catch (TQueryException e) { 148 throw new PMException(e); 149 } catch (TNoSuchXMLObjectException e) { 150 throw new PMException(e); 151 } catch (TIteratorException e) { 152 throw new PMException(e); 153 } catch (JAXBException e) { 154 throw new PMException(e); 155 } finally { 156 if (conn != null) { try { conn.close(); } catch (Throwable ignore) {} } 157 setJAXBContext(null); 158 DocumentDefaultHandler.setUnmarshallerHandler(null); 159 ElementDefaultHandler.initData(); 160 } 161 } 162 163 public void insert(Element pElement) throws PMException { 164 TConnection conn = null; 165 try { 166 setJAXBContext(getManager().getFactory()); 167 conn = getTConnection(); 168 TXMLObjectAccessor accessor = conn.newXMLObjectAccessor(TAccessLocation.newInstance(collection), model); 169 accessor.insert(getTXMLObject((InoObject) pElement)); 170 } catch (TServerNotAvailableException e) { 171 throw new PMException(e); 172 } catch (NamingException e) { 173 throw new PMException(e); 174 } catch (TInsertException e) { 175 throw new PMException(e); 176 } finally { 177 if (conn != null) { try { conn.close(); } catch (Throwable ignore) {} } 178 setJAXBContext(null); 179 } 180 } 181 182 public void update(Element pElement) throws PMException { 183 TConnection conn = null; 184 try { 185 setJAXBContext(getManager().getFactory()); 186 conn = getTConnection(); 187 TXMLObjectAccessor accessor = conn.newXMLObjectAccessor(TAccessLocation.newInstance(collection), model); 188 accessor.update(getTXMLObject((InoObject) pElement)); 189 } catch (TServerNotAvailableException e) { 190 throw new PMException(e); 191 } catch (NamingException e) { 192 throw new PMException(e); 193 } catch (TUpdateException e) { 194 throw new PMException(e); 195 } finally { 196 if (conn != null) { try { conn.close(); } catch (Throwable ignore) {} } 197 setJAXBContext(null); 198 } 199 } 200 201 public void delete(Element pElement) throws PMException { 202 TConnection conn = null; 203 try { 204 setJAXBContext(getManager().getFactory()); 205 conn = getTConnection(); 206 TXMLObjectAccessor accessor = conn.newXMLObjectAccessor(TAccessLocation.newInstance(collection), model); 207 accessor.delete(getTXMLObject((InoObject) pElement)); 208 } catch (TServerNotAvailableException e) { 209 throw new PMException(e); 210 } catch (NamingException e) { 211 throw new PMException(e); 212 } catch (TDeleteException e) { 213 throw new PMException(e); 214 } finally { 215 if (conn != null) { try { conn.close(); } catch (Throwable ignore) {} } 216 setJAXBContext(null); 217 } 218 } 219 } 220 | Popular Tags |