031377cb064e0360c1b49db7cdeaa64635692280
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / cdm / remote / CdmRemoteSourceBase.java
1 /**
2 * Copyright (C) 2014 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.remote;
10
11 import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
12
13 import eu.etaxonomy.cdm.api.service.IDatabaseService;
14 import eu.etaxonomy.cdm.config.CdmSource;
15 import eu.etaxonomy.cdm.config.CdmSourceException;
16 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
17
18 /**
19 * Base class representing a CDM remote source.
20 * This class handles all the configuration relating to the remoting aspect of
21 * a CDM source.
22 *
23 */
24 public class CdmRemoteSourceBase extends CdmSource implements ICdmRemoteSource {
25
26 private String contextPath;
27 private String baseUrl;
28
29 private IDatabaseService databaseService;
30
31 /**
32 * Constructs a CdmRemoteSourceBase object with default values.
33 *
34 */
35 protected CdmRemoteSourceBase() {
36 setName("default");
37 setServer("127.0.0.1");
38 setPort(8080);
39 setContextPath("");
40 setNomenclaturalCode(NomenclaturalCode.ICNAFP);
41 }
42
43 /**
44 * Constructs a CdmRemoteSourceBase
45 *
46 * @param name
47 * @param server
48 * @param port
49 * @param contextPath
50 * @param nomenclaturalCode
51 */
52 protected CdmRemoteSourceBase(String name, String server, int port, String contextPath, NomenclaturalCode nomenclaturalCode) {
53 setName(name);
54 setServer(server);
55 setPort(port);
56 setContextPath(contextPath);
57 setNomenclaturalCode(nomenclaturalCode);
58 if(contextPath == null || contextPath.equals("")) {
59 setBaseUrl("http://" + server + ":" + String.valueOf(port));
60 } else {
61 setBaseUrl("http://" + server + ":" + String.valueOf(port) + "/" + contextPath);
62 }
63 // the database service needs to be initialised (before the spring
64 // application context initialsation) since it is required to
65 // to make queries related to the source database
66 HttpInvokerProxyFactoryBean proxy = new HttpInvokerProxyFactoryBean();
67 proxy.setServiceInterface(IDatabaseService.class);
68 proxy.setServiceUrl(getBaseUrl() + "/remoting/database.service");
69 proxy.afterPropertiesSet();
70 databaseService = (IDatabaseService) proxy.getObject();
71 }
72
73 /* (non-Javadoc)
74 * @see eu.etaxonomy.cdm.remote.ICdmRemoteSource#getBaseUrl()
75 */
76 @Override
77 public String getBaseUrl() {
78 return baseUrl;
79 }
80
81 /**
82 * Sets the base url for the http-invoker services as listed in
83 * httpInvokerServicesClients.xml.
84 * e.g. for 'http://127.0.0.1:8080/col/remoting/common.service', the
85 * base url would be 'http://127.0.0.1:8080/col'
86 *
87 * @param baseUrl
88 */
89 public void setBaseUrl(String baseUrl) {
90 this.baseUrl = baseUrl;
91 }
92
93 /* (non-Javadoc)
94 * @see eu.etaxonomy.cdm.remote.ICdmRemoteSource#getContextPath()
95 */
96 @Override
97 public String getContextPath() {
98 return contextPath;
99 }
100
101 /**
102 * Sets the context path.
103 * e.g. for 'http://127.0.0.1:8080/col/remoting/common.service', the
104 * context path would be 'col'
105 *
106 * @param contextPath
107 */
108 public void setContextPath(String contextPath) {
109 this.contextPath = contextPath;
110 }
111
112 /* (non-Javadoc)
113 * @see eu.etaxonomy.cdm.config.CdmSource#getDbSchemaVersion()
114 */
115 @Override
116 public String getDbSchemaVersion() throws CdmSourceException {
117 return databaseService.getDbSchemaVersion();
118
119 }
120
121 /* (non-Javadoc)
122 * @see eu.etaxonomy.cdm.config.CdmSource#isDbEmpty()
123 */
124 @Override
125 public boolean isDbEmpty() throws CdmSourceException {
126 return databaseService.isDbEmpty();
127
128 }
129
130 /* (non-Javadoc)
131 * @see eu.etaxonomy.cdm.config.CdmSource#checkConnection()
132 */
133 @Override
134 public boolean checkConnection() throws CdmSourceException {
135 // assuming that database service works implies
136 // the connection is up
137 // if no exception is thrown then we assume that the
138 // connection is up
139 // FIXME:Remoting is this really correct?
140 databaseService.getDbSchemaVersion();
141
142 return true;
143 }
144
145 /* (non-Javadoc)
146 * @see eu.etaxonomy.cdm.config.CdmSource#getConnectionMessage()
147 */
148 @Override
149 public String getConnectionMessage() {
150 return "Conncting to Remote CDM Server " + getName();
151 }
152
153 }