KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > resource > deployment > ds > DsObjectModelFactory


1 /*
2  * JBoss, Home of Professional Open Source
3  * Copyright 2005, JBoss Inc., and individual contributors as indicated
4  * by the @authors tag. See the copyright.txt in the distribution for a
5  * full listing of individual contributors.
6  *
7  * This is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * This software is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this software; if not, write to the Free
19  * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21  */

22 package org.jboss.resource.deployment.ds;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.jboss.logging.Logger;
28 import org.jboss.resource.deployment.ds.metadata.AbstractConnectionFactoryMetaData;
29 import org.jboss.resource.deployment.ds.metadata.AvailabilitySupportMetaData;
30 import org.jboss.resource.deployment.ds.metadata.ConnectionFactoryMetaData;
31 import org.jboss.resource.deployment.ds.metadata.ConnectionFactoryMetaDataFactory;
32 import org.jboss.resource.deployment.ds.metadata.ConnectionPoolMetaData;
33 import org.jboss.resource.deployment.ds.metadata.DataSourceMetaData;
34 import org.jboss.resource.deployment.ds.metadata.DsDefinitionMetaData;
35 import org.jboss.resource.deployment.ds.metadata.DsMetaData;
36 import org.jboss.resource.deployment.ds.metadata.DsPropertyMetaData;
37 import org.jboss.resource.deployment.ds.metadata.PoolCriteriaMetaData;
38 import org.jboss.resource.deployment.ds.metadata.SqlSupportMetaData;
39 import org.jboss.resource.deployment.ds.metadata.TransactionSupportFactory;
40 import org.jboss.resource.deployment.ds.metadata.TransactionSupportMetaData;
41 import org.jboss.xb.binding.ObjectModelFactory;
42 import org.jboss.xb.binding.UnmarshallingContext;
43 import org.xml.sax.Attributes JavaDoc;
44
45 /**
46  * Object Model Factory for a DataSource and ConnectionFactory deployment.
47  *
48  * @author <a HREF="weston.price@jboss.com">Weston Price</a>
49  * @version $Revision: 45634 $
50  */

51 public class DsObjectModelFactory implements ObjectModelFactory
52 {
53    /** The logger */
54    private static final Logger log = Logger.getLogger(DsObjectModelFactory.class);
55
56    /** The connectionPoolElementNames */
57    private static final List JavaDoc connectionPoolElementNames = new ArrayList JavaDoc();
58
59    /** The sqlSupportElementNames */
60    private static final List JavaDoc sqlSupportElementNames = new ArrayList JavaDoc();
61
62    /** The criteriaSupportElementNames */
63    private static final List JavaDoc criteriaSupportElementNames = new ArrayList JavaDoc();
64
65    /** The txnSupportElementnames */
66    private static final List JavaDoc txnSupportElementnames = new ArrayList JavaDoc();
67
68    private static final List JavaDoc availablitySupportElementNames = new ArrayList JavaDoc();
69
70    static
71    {
72
73       //Element names for ConnectionPool support
74
connectionPoolElementNames.add("min-pool-size");
75       connectionPoolElementNames.add("max-pool-size");
76       connectionPoolElementNames.add("blocking-timeout-millis");
77       connectionPoolElementNames.add("idle-timeout-minutes");
78       connectionPoolElementNames.add("prefill");
79       connectionPoolElementNames.add("background-validation");
80       connectionPoolElementNames.add("background-validtion-interval");
81       connectionPoolElementNames.add("validate-on-match");
82       connectionPoolElementNames.add("no-tx-separate-pools");
83
84       //Element names for SqlSupport
85
sqlSupportElementNames.add("new-connection-sql");
86       sqlSupportElementNames.add("check-valid-connection-sql");
87       sqlSupportElementNames.add("valid-connection-checker-class-name");
88       sqlSupportElementNames.add("exception-sorter-class-name");
89       sqlSupportElementNames.add("prepared-statement-cache-size");
90       sqlSupportElementNames.add("share-prepared-statements");
91       sqlSupportElementNames.add("track-statements");
92       sqlSupportElementNames.add("query-timeout");
93       sqlSupportElementNames.add("set-tx-query-timeout");
94       sqlSupportElementNames.add("transaction-isolation");
95
96       criteriaSupportElementNames.add("application-managed-security");
97       criteriaSupportElementNames.add("security-domain");
98       criteriaSupportElementNames.add("security-domain-and-application");
99
100       txnSupportElementnames.add("no-tx-datasource");
101       txnSupportElementnames.add("local-tx-datasource");
102       txnSupportElementnames.add("xa-datasource");
103       txnSupportElementnames.add("no-tx-connection-factory");
104       txnSupportElementnames.add("tx-connection-factory");
105       txnSupportElementnames.add("ha-local-tx-datasource");
106       txnSupportElementnames.add("ha-xa-datasource");
107
108       availablitySupportElementNames.add("ha-local-tx-datasource");
109       availablitySupportElementNames.add("ha-xa-datasource");
110
111    }
112
113    /** Trace enabled */
114    private boolean trace = log.isTraceEnabled();
115
116    public Object JavaDoc newRoot(Object JavaDoc root, UnmarshallingContext navigator, String JavaDoc namespaceURI, String JavaDoc localName,
117          Attributes JavaDoc attrs)
118    {
119       checkRoot(localName);
120       DsMetaData md = new DsMetaData();
121       final DsDefinitionMetaData dsdmd = ConnectionFactoryMetaDataFactory.getDefinitionMetaData(localName);
122       md.setDsDefinitionMetaData(dsdmd);
123       return dsdmd;
124    }
125
126    public Object JavaDoc newChild(DsDefinitionMetaData md, UnmarshallingContext navigator, String JavaDoc namespaceURI,
127          String JavaDoc localName, Attributes JavaDoc attrs)
128    {
129       AbstractConnectionFactoryMetaData abmd = ConnectionFactoryMetaDataFactory.getFactoryMetaData(localName);
130       md.addDsDefinition(abmd);
131       return abmd;
132
133    }
134
135    public Object JavaDoc addChild(DsDefinitionMetaData md, ConnectionFactoryMetaData cfmd, UnmarshallingContext navigator,
136          String JavaDoc namespaceURI, String JavaDoc localName)
137    {
138       if (txnSupportElementnames.contains(localName))
139       {
140          TransactionSupportMetaData txmd = handleTransactionSupportMetaData(cfmd, localName);
141          cfmd.setTxnSupport(txmd);
142          
143       }
144
145       return cfmd;
146
147    }
148
149    public Object JavaDoc addChild(DsDefinitionMetaData md, DataSourceMetaData dsmd, UnmarshallingContext navigator,
150          String JavaDoc namespaceURI, String JavaDoc localName)
151    {
152
153       if (txnSupportElementnames.contains(localName))
154       {
155
156          TransactionSupportMetaData txmd = handleTransactionSupportMetaData(dsmd, localName);
157          dsmd.setTxnSupport(txmd);
158     
159       }
160
161       if (availablitySupportElementNames.contains(localName))
162       {
163          AvailabilitySupportMetaData amd = handleAvailabilitySupportMetaData(dsmd, localName);
164          dsmd.setAvailabilitySupport(amd);
165
166       }
167       
168       if (localName.equals("no-tx-datasource") || localName.equals("local-tx-datasource"))
169       {
170          dsmd.setRarName("jboss-local-jdbc.rar");
171          dsmd.setConnectionDefintion("javax.sql.DataSource");
172          
173       }
174       else if (localName.equals("xa-tx-datasource"))
175       {
176          dsmd.setRarName("jboss-xa-jdbc.rar");
177          dsmd.setConnectionDefintion("javax.sql.DataSource");
178
179       }
180       else if (localName.equals("ha-local-tx-datasource"))
181       {
182          dsmd.setRarName("jboss-ha-local-jdbc.rar");
183          dsmd.setConnectionDefintion("javax.sql.DataSource");
184
185       }
186       else if (localName.equals("ha-xa-datasource"))
187       {
188          dsmd.setRarName("jboss-ha-xa-jdbc.rar");
189          dsmd.setConnectionDefintion("javax.sql.DataSource");
190
191       }
192
193       
194       return dsmd;
195
196    }
197
198    public Object JavaDoc newChild(ConnectionFactoryMetaData cfmd, UnmarshallingContext navigator, String JavaDoc namespaceURI,
199          String JavaDoc localName, Attributes JavaDoc attrs)
200    {
201
202       if (localName.equals("config-property"))
203       {
204          DsPropertyMetaData pmd = handlePropertyMetaData(localName, attrs);
205          cfmd.addProperty(pmd);
206          return pmd;
207       }
208       else if (connectionPoolElementNames.contains(localName))
209       {
210          return handlePoolMetaData(cfmd);
211
212       }
213       else if (criteriaSupportElementNames.contains(localName))
214       {
215          return handlePoolCriteriaMetaData(cfmd, localName);
216
217       }
218       else if (localName.equals("local-transaction") || localName.equals("xa-transaction"))
219       {
220          //Note, this is probably already set, but because the element structure
221
//is fundamentally different than datasource, we have to hack this
222
cfmd.setTxnSupport(TransactionSupportFactory.getTransactionSupport(localName));
223
224       }
225
226       return null;
227    }
228
229    public Object JavaDoc newChild(DataSourceMetaData dsmd, UnmarshallingContext navigator, String JavaDoc namespaceURI,
230          String JavaDoc localName, Attributes JavaDoc attrs)
231    {
232
233       if (localName.equals("connection-property") || localName.equals("xa-datasource-property"))
234       {
235          DsPropertyMetaData pmd = handlePropertyMetaData(localName, attrs);
236          dsmd.addProperty(pmd);
237          return pmd;
238
239       }
240
241       else if (sqlSupportElementNames.contains(localName))
242       {
243          return handleSqlSupportMetaData(dsmd);
244
245       }
246       else if (connectionPoolElementNames.contains(localName))
247       {
248          return handlePoolMetaData(dsmd);
249
250       }
251       else if (criteriaSupportElementNames.contains(localName))
252       {
253          return handlePoolCriteriaMetaData(dsmd, localName);
254
255       }
256
257       return null;
258
259    }
260
261    public void setValue(final DsPropertyMetaData dsmd, UnmarshallingContext navigator, String JavaDoc namespaceURI,
262          String JavaDoc localName, String JavaDoc value)
263    {
264       dsmd.setValue(value);
265
266    }
267
268    public void setValue(final ConnectionFactoryMetaData cfmd, UnmarshallingContext navigator, String JavaDoc namespaceURI,
269          String JavaDoc localName, String JavaDoc value)
270    {
271       if (localName.equals("jndi-name"))
272       {
273          cfmd.setJndiName(value);
274
275       }
276       else if (localName.equals("use-java-context"))
277       {
278          cfmd.setUseJavaConext(Boolean.valueOf(localName).booleanValue());
279
280       }
281       else if (localName.equals("connection-definition"))
282       {
283          cfmd.setConnectionDefintion(localName);
284       }
285
286    }
287
288    public void setValue(final DataSourceMetaData dsmd, UnmarshallingContext navigator, String JavaDoc namespaceURI,
289          String JavaDoc localName, String JavaDoc value)
290    {
291
292       if (localName.equals("jndi-name"))
293       {
294          dsmd.setJndiName(value);
295
296       }
297       else if (localName.equals("use-java-context"))
298       {
299          dsmd.setUseJavaConext(Boolean.valueOf(localName).booleanValue());
300
301       }
302       else if (localName.equals("connection-definition"))
303       {
304          dsmd.setConnectionDefintion(localName);
305       }
306       else if (localName.equals("xa-datasource-class"))
307       {
308          dsmd.setDriverClass(value);
309
310       }
311       else if (localName.equals("isSameRM-override-value"))
312       {
313          dsmd.setSameRMOverride(Boolean.valueOf(value).booleanValue());
314
315       }
316       else if (localName.equals("type-mapping"))
317       {
318          dsmd.setTypeMapping(value);
319
320       }
321       else if(localName.equals("username"))
322       {
323          dsmd.setUserName(value);
324       }
325       else if(localName.equals("password"))
326       {
327          dsmd.setPassWord(value);
328          
329       }
330       else if(localName.equals("connection-url"))
331       {
332          dsmd.setConnectionUrl(value);
333          
334       }else if(localName.equals("driver-class") || localName.equals("xa-datasource-class"))
335       {
336          dsmd.setDriverClass(value);
337          
338       }else if(localName.equals("url-delimeter"))
339       {
340          
341          dsmd.getAvailabilitySupport().setUrlDelimeter(value);
342          
343       }else if(localName.equals("url-property"))
344       {
345          dsmd.getAvailabilitySupport().setUrlProperty(value);
346          
347       }
348       
349       else
350       {
351          //TODO throw here
352
}
353
354    }
355
356    public void setValue(final ConnectionPoolMetaData cpmd, UnmarshallingContext navigator, String JavaDoc namespaceURI,
357          String JavaDoc localName, String JavaDoc value)
358    {
359
360       if (localName.equals("min-pool-size"))
361       {
362          cpmd.setMinPoolSize(Integer.valueOf(value).intValue());
363       }
364       else if (localName.equals("max-pool-size"))
365       {
366          cpmd.setMaxPoolSize(Integer.valueOf(value).intValue());
367       }
368       else if (localName.equals("blocking-timeout-millis"))
369       {
370          cpmd.setBlockingTimeoutMilliseconds(Long.valueOf(value).longValue());
371       }
372       else if (localName.equals("idle-timeout-minutes"))
373       {
374          cpmd.setIdleTimeoutMinutes(Integer.valueOf(value).intValue());
375
376       }
377       else if (localName.equals("security-damain") || localName.equals("application-managed-security")
378             || localName.equals("security-domain-and-application"))
379       {
380          cpmd.setPoolCriteria(PoolCriteriaMetaData.fromDsElement(localName));
381
382       }
383       else if (localName.equals("background-validation"))
384       {
385          cpmd.setBackgroundValidation(Boolean.valueOf(value).booleanValue());
386
387       }
388       else if (localName.equals("background-validation-interval"))
389       {
390          cpmd.setBackgroundValidationInterval(Integer.valueOf(value).intValue());
391
392       }
393       else if (localName.equals("no-tx-separate-pools"))
394       {
395          cpmd.setNoTxSeperatePool(Boolean.valueOf(value).booleanValue());
396
397       }
398       else if (localName.equals("prefill"))
399       {
400          cpmd.setPrefill(Boolean.valueOf(value).booleanValue());
401
402       }
403
404       else
405       {
406
407          throw new IllegalArgumentException JavaDoc("Unknown connection pool setValue: nuri=" + namespaceURI + " localName="
408                + localName + " value=" + value);
409
410       }
411
412    }
413
414    public void setValue(final SqlSupportMetaData sqlmd, UnmarshallingContext navigator, String JavaDoc namespaceURI,
415          String JavaDoc localName, String JavaDoc value)
416    {
417
418       if (localName.equals("prepared-statement-cache-size"))
419       {
420          sqlmd.setPreparedStatementCacheSize(new Integer JavaDoc(value).intValue());
421
422       }
423       else if (localName.equals("share-prepared-statements"))
424       {
425          sqlmd.setShareStatements(Boolean.valueOf(value).booleanValue());
426       }
427       else if (localName.equals("track-statements"))
428       {
429          sqlmd.setTrackStatements(Boolean.valueOf(value).booleanValue());
430
431       }
432       else if (localName.equals("new-connection-sql"))
433       {
434
435          sqlmd.setNewConnectionSql(value);
436
437       }
438       else if (localName.equals("check-valid-connection-sql"))
439       {
440
441          sqlmd.setCheckValidConnectionSql(value);
442
443       }
444       else if (localName.equals("valid-connection-checker-class-name"))
445       {
446
447          sqlmd.setValidConnectionCheckerClassName(value);
448
449       }
450       else if (localName.equals("exception-sorter-class-name"))
451       {
452
453          sqlmd.setExceptionSorterClassName(value);
454
455       }
456       else if (localName.equals("query-timeout"))
457       {
458          sqlmd.setQueryTimeout(Integer.valueOf(value).intValue());
459
460       }
461       else if (localName.equals("set-tx-query-timeout"))
462       {
463          sqlmd.setTxQueryTimeout(true);
464
465       }else if(localName.equals("track-statements"))
466       {
467         
468          if(value.equals("nowarn"))
469          {
470             sqlmd.setTrackStatements(Boolean.TRUE.booleanValue());
471             sqlmd.setTrackStatementsNoWarn(Boolean.TRUE.booleanValue());
472             
473          }else if(value.equals("true"))
474          {
475             sqlmd.setTrackStatements(Boolean.TRUE.booleanValue());
476             
477          }
478             
479          
480       }
481    }
482
483    public Object JavaDoc completeRoot(Object JavaDoc root, UnmarshallingContext ctx, String JavaDoc uri, String JavaDoc name)
484    {
485       return root;
486    }
487
488    private void checkRoot(String JavaDoc localName)
489    {
490       if (!localName.equals("datasources") && !localName.equals("connection-factories"))
491       {
492
493          throw new IllegalStateException JavaDoc(
494                "Unexpected root element: was expecting 'datasources' or 'connection-factories' but got '" + localName
495                      + "'");
496       }
497
498    }
499
500    private DsPropertyMetaData handlePropertyMetaData(String JavaDoc localName, Attributes JavaDoc attrs)
501    {
502       DsPropertyMetaData pmd = new DsPropertyMetaData();
503
504       if (attrs != null && attrs.getLength() > 0)
505       {
506          for (int i = 0; i < attrs.getLength(); i++)
507          {
508             if (attrs.getLocalName(i).equals("name"))
509             {
510                pmd.setName(attrs.getValue(i));
511
512             }
513             if (attrs.getLocalName(i).equals("type"))
514             {
515                pmd.setType(attrs.getValue(i));
516
517             }
518          }
519       }
520       return pmd;
521    }
522
523    private ConnectionPoolMetaData handlePoolCriteriaMetaData(ConnectionFactoryMetaData cfmd, String JavaDoc localName)
524    {
525
526       ConnectionPoolMetaData cpmd = cfmd.getPoolMetaData();
527
528       if (cpmd == null)
529       {
530          cpmd = new ConnectionPoolMetaData();
531       }
532
533       if (cpmd.getPoolCriteria().equals(PoolCriteriaMetaData.NOTHING))
534       {
535          PoolCriteriaMetaData pcmd = PoolCriteriaMetaData.fromDsElement(localName);
536          cpmd.setPoolCriteria(pcmd);
537
538       }
539
540       return cpmd;
541
542    }
543
544    private ConnectionPoolMetaData handlePoolMetaData(ConnectionFactoryMetaData cfmd)
545    {
546
547       ConnectionPoolMetaData cpmd = cfmd.getPoolMetaData();
548
549       if (cpmd == null)
550       {
551
552          cpmd = new ConnectionPoolMetaData();
553          cfmd.setPoolMetaData(cpmd);
554
555       }
556
557       return cpmd;
558
559    }
560
561    private SqlSupportMetaData handleSqlSupportMetaData(DataSourceMetaData dsmd)
562    {
563
564       SqlSupportMetaData sqlmd = dsmd.getSqlSupport();
565
566       if (sqlmd == null)
567       {
568          sqlmd = new SqlSupportMetaData();
569          dsmd.setSqlSupportMetaData(sqlmd);
570
571       }
572
573       return sqlmd;
574
575    }
576
577    private TransactionSupportMetaData handleTransactionSupportMetaData(ConnectionFactoryMetaData cfmd, String JavaDoc localName)
578    {
579       TransactionSupportMetaData txmd = cfmd.getTxnSupport();
580
581       if (txmd == null)
582       {
583
584          txmd = TransactionSupportMetaData.fromString(localName);
585          cfmd.setTxnSupport(txmd);
586
587       }
588       
589       return txmd;
590    
591    }
592
593    private AvailabilitySupportMetaData handleAvailabilitySupportMetaData(DataSourceMetaData dsmd, String JavaDoc localName)
594    {
595       AvailabilitySupportMetaData amd = dsmd.getAvailabilitySupport();
596
597       if (amd == null || amd.equals(AvailabilitySupportMetaData.NONE))
598       {
599          amd = AvailabilitySupportMetaData.fromString(localName);
600          
601       }
602       
603       return amd;
604
605    }
606 }
607
Popular Tags