View Javadoc

1   /*
2    ORDI - Ontology Repository and Data Integration
3   
4    Copyright (c) 2004-2007, OntoText Lab. / SIRMA
5   
6    This library is free software; you can redistribute it and/or modify it under
7    the terms of the GNU Lesser General Public License as published by the Free
8    Software Foundation; either version 2.1 of the License, or (at your option)
9    any later version.
10   This library is distributed in the hope that it will be useful, but WITHOUT
11   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
13   details.
14   You should have received a copy of the GNU Lesser General Public License along
15   with this library; if not, write to the Free Software Foundation, Inc.,
16   59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17   */
18  package com.ontotext.ordi.rdbms;
19  
20  import java.io.File;
21  import java.util.Map;
22  
23  import com.ontotext.ordi.exception.ORDIRuntimeException;
24  import com.ontotext.ordi.exception.ORDIWarning;
25  import com.ontotext.ordi.mapper.model.MapperDescriptor;
26  import com.ontotext.ordi.mapper.processor.N3MapperDescriptorImpl;
27  import com.ontotext.ordi.mapper.processor.Processor;
28  import com.ontotext.ordi.tripleset.TConnection;
29  import com.ontotext.ordi.tripleset.TFactory;
30  import com.ontotext.ordi.tripleset.impl.TFactoryImpl;
31  import com.ontotext.ordi.tripleset.impl.TSourceImpl;
32  
33  public class RDBMSAdapter extends TSourceImpl {
34  
35      public static final String MAPPER_DESCRIPTOR = "mapper-descriptor";
36  
37      private TFactory factory;
38      private Processor processor;
39  
40      public RDBMSAdapter(Map<Object, Object> props) {
41          super(props);
42          if (props.containsKey(MAPPER_DESCRIPTOR) == false) {
43              throw new ORDIRuntimeException(
44                      "Specify the mapper descriptor parameter!");
45          }
46          Object descriptor = props.get(MAPPER_DESCRIPTOR);
47          if (descriptor instanceof File) {
48              File file = (File) descriptor;
49              if (file.exists() == false) {
50                  throw new ORDIRuntimeException(String.format(
51                          "Invalid file location %!", file.getAbsolutePath()));
52              }
53              processor = new Processor(new N3MapperDescriptorImpl(file));
54          } else if (descriptor instanceof String) {
55              File file = new File((String) descriptor);
56              if (file.exists() == false) {
57                  throw new ORDIRuntimeException(String.format(
58                          "Invalid file location %!", file.getAbsolutePath()));
59              }
60              processor = new Processor(new N3MapperDescriptorImpl(file));
61          } else if (descriptor instanceof MapperDescriptor) {
62              processor = new Processor((MapperDescriptor) descriptor);
63          }
64  
65          else {
66              throw new ORDIRuntimeException(
67                      "The mapper descriptor parameter has invalid type!");
68          }
69      }
70  
71      public TConnection getConnection() {
72          return new RDBMSConnection(this, processor);
73      }
74  
75      public TConnection getConnection(String user, String pass) {
76          return getConnection();
77      }
78  
79      public void shutdown() {
80          ORDIWarning newWarning = new ORDIWarning(
81                  "The adapter does not support shutdown!");
82          newWarning.setNextWarning(this.warning);
83          this.warning = newWarning;
84      }
85      
86      public boolean isShutdown() {
87          return true;
88      }
89  
90      public synchronized TFactory getTriplesetFactory() {
91          if (factory == null) {
92              factory = new TFactoryImpl();
93          }
94          return factory;
95      }
96  
97      public Processor getProcessor() {
98          return processor;
99      }
100 }