View Javadoc

1   package com.ontotext.ordi.wsmo4rdf.impl;
2   
3   import java.util.HashMap;
4   import java.util.Map;
5   
6   import org.apache.log4j.Logger;
7   import org.wsmo.factory.DataFactory;
8   import org.wsmo.factory.Factory;
9   import org.wsmo.factory.LogicalExpressionFactory;
10  import org.wsmo.factory.WsmoFactory;
11  
12  import com.ontotext.ordi.exception.ORDIException;
13  import com.ontotext.ordi.exception.ORDIWarning;
14  import com.ontotext.ordi.tripleset.DataSourceImpl;
15  import com.ontotext.ordi.tripleset.TSource;
16  import com.ontotext.ordi.wsmo4rdf.WsmoConnection;
17  import com.ontotext.ordi.wsmo4rdf.WsmoSource;
18  
19  public class WsmoSourceImpl extends DataSourceImpl implements WsmoSource {
20  
21      private TSource ordi;
22      private WsmoFactory wsmoFactory;
23      private LogicalExpressionFactory leFactory;
24      private Logger logger = Logger.getLogger(WsmoSource.class);
25      private boolean isShutDown = false;
26      
27      public WsmoSourceImpl(Map<Object, Object> params) {
28          super(params);
29          initialize(params);
30      }
31      
32      public WsmoConnection getConnection() {
33          logger.info("Getting a new WsmoConnection.");
34          return new WsmoConnectionImpl(this, ordi.getConnection(), wsmoFactory, leFactory);
35      }
36  
37      public WsmoConnection getConnection(String user, String pass) {
38          return getConnection();
39      }
40  
41      public boolean isWrapperFor(Class<?> iface) throws ORDIException {
42          if (TSource.class.isAssignableFrom(iface))
43              return true;
44          return false;
45      }
46  
47      @SuppressWarnings("unchecked")
48      public <T> T unwrap(Class<T> iface) throws ORDIException {
49          if (TSource.class.isAssignableFrom(iface))
50              return (T) ordi;
51          return null;
52      }
53  
54      public void clearWarnings() {
55          ordi.clearWarnings();
56      }
57  
58      public ORDIWarning getWarning() {
59          return ordi.getWarning();
60      }
61  
62      public void shutdown() {
63          logger.info("Shuttdowning the WsmoSource.");
64          ordi.shutdown();
65          isShutDown = true;
66      }
67      
68      public boolean isShutdown() {
69          return ordi.isShutdown();
70      }
71      
72      protected void finalize() throws Throwable {
73          if (isShutDown == false)
74              shutdown();
75          super.finalize();
76      }
77      
78      private void initialize(Map<Object, Object> params) {
79          if (params == null) {
80              params = new HashMap<Object, Object>();
81          }
82          Object ordiValue = params.get(WsmoSource.ORDI_SOURCE);
83          if (ordiValue instanceof String) {
84              ordi = com.ontotext.ordi.Factory.createTSource(params);
85          } else if (ordiValue instanceof TSource) {
86              ordi = (TSource) ordiValue;
87          } else {
88              logger.info("ORDI implementation for WSMO4RDF was not specified! "
89                      + "Using the default implementation.");
90              ordi = com.ontotext.ordi.Factory.createDefaultTSource();
91          }
92  
93          // Initialize WSMO factories
94          Object factory = params.get(Factory.WSMO_FACTORY);
95          if (factory == null || factory instanceof WsmoFactory == false) {
96              factory = Factory.createWsmoFactory(null);
97          }
98          wsmoFactory = (WsmoFactory) factory;
99          factory = params.get(Factory.LE_FACTORY);
100         if (factory == null
101                 || factory instanceof LogicalExpressionFactory == false) {
102             factory = Factory.createLogicalExpressionFactory(null);
103         }
104         leFactory = (LogicalExpressionFactory) factory;
105         factory = params.get(Factory.DATA_FACTORY);
106         if (factory != null && factory instanceof DataFactory) {
107             Util.setDataFactory((DataFactory) factory);
108         }
109         logger.info("New WsmoSource initialized.");
110     }
111 }