1 package com.ontotext.ordi.wsmo4rdf.remote.client;
2
3 import java.util.Map;
4
5 import org.apache.log4j.Logger;
6
7 import com.ontotext.ordi.exception.ORDIException;
8 import com.ontotext.ordi.exception.ORDIWarning;
9 import com.ontotext.ordi.wsmo4rdf.WsmoConnection;
10 import com.ontotext.ordi.wsmo4rdf.WsmoSource;
11 import com.ontotext.ordi.wsmo4rdf.remote.client.src.AuthenticationFailedException_Exception;
12 import com.ontotext.ordi.wsmo4rdf.remote.client.src.NonExistingEntryException_Exception;
13 import com.ontotext.ordi.wsmo4rdf.remote.client.src.ServiceLimitExceededException_Exception;
14
15 public class RemoteWsmoSource implements WsmoSource {
16 private static final Logger logger = Logger.getLogger(RemoteWsmoSource.class);
17
18 private final int id;
19 private final RemoteWsmo4rdfGate gate;
20
21 public RemoteWsmoSource(final int remoteId,
22 final RemoteWsmo4rdfGate gate) {
23 this.id = remoteId;
24 this.gate = gate;
25 }
26
27 @Override
28 public void finalize() {
29 logger.debug("Requested destruction of the " + RemoteWsmoSource.class.getName() +
30 " with ID=" + this.id + " from GarbageCollector by finalize()");
31 this.gate.getFactoryService().destroyWsmoSource(this.id);
32 }
33
34 int getId() {
35 return this.id;
36 }
37
38 public RemoteWsmoConnection getConnection() {
39 int conId = Integer.MIN_VALUE;
40 try {
41 conId = this.gate.getSourceService().getConnection(this.id);
42 } catch (NonExistingEntryException_Exception e) {
43 logNoSuchWsmoSourceOnServer();
44 return null;
45 } catch (ServiceLimitExceededException_Exception e) {
46 logger.error("Server cannot handle any more new connections.");
47 return null;
48 }
49 assert(conId != Integer.MIN_VALUE);
50 RemoteWsmoConnection con = new RemoteWsmoConnection(conId, this.gate);
51 return con;
52 }
53
54 public WsmoConnection getConnection(String user, String pass) {
55 int conId = 0;
56 try {
57 conId = this.gate.getSourceService().getConnectionAuth(this.id, user, pass);
58 } catch (NonExistingEntryException_Exception e) {
59 logNoSuchWsmoSourceOnServer();
60 return null;
61 } catch (ServiceLimitExceededException_Exception e) {
62 logger.error("Server cannot handle any more new connections.");
63 return null;
64 } catch (AuthenticationFailedException_Exception e) {
65 logger.error("Authentication failure for user \"" + user + "\"");
66 return null;
67 }
68 assert(conId != 0);
69 WsmoConnection con = new RemoteWsmoConnection(conId, this.gate);
70 return con;
71 }
72
73
74 public Map<Object, Object> getMetaData() {
75 throw new UnsupportedOperationException();
76 }
77
78
79 public boolean isWrapperFor(Class<?> iface) throws ORDIException {
80 throw new UnsupportedOperationException();
81 }
82
83
84 public <T> T unwrap(Class<T> iface) throws ORDIException {
85 throw new UnsupportedOperationException();
86 }
87
88
89 public void clearWarnings() {
90 try {
91 this.gate.getSourceService().clearWarnings(this.id);
92 } catch (NonExistingEntryException_Exception e) {
93 logNoSuchWsmoSourceOnServer();
94 e.printStackTrace();
95 }
96 }
97
98
99 public ORDIWarning getWarning() {
100 throw new UnsupportedOperationException();
101 }
102
103
104 public boolean isShutdown() {
105 return false;
106
107 }
108
109 public void shutdown() {
110 try {
111 this.gate.getSourceService().shutdown(this.id);
112 } catch (NonExistingEntryException_Exception e) {
113 logNoSuchWsmoSourceOnServer();
114 e.printStackTrace();
115 }
116 }
117
118 private void logNoSuchWsmoSourceOnServer() {
119 logger.error("There is no " + WsmoSource.class.getName() + " with ID=" + this.id + " on the server.");
120 }
121
122 }