ref #6118 catching exception during checkConnection
[taxeditor.git] / eu.etaxonomy.taxeditor.cdmlib / src / main / java / eu / etaxonomy / taxeditor / remoting / source / 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.taxeditor.remoting.source;
10
11 import java.util.Map;
12
13 import org.springframework.remoting.RemoteAccessException;
14
15 import eu.etaxonomy.cdm.api.application.CdmApplicationRemoteConfiguration;
16 import eu.etaxonomy.cdm.api.service.IMetadataService;
17 import eu.etaxonomy.cdm.config.CdmSource;
18 import eu.etaxonomy.cdm.config.CdmSourceException;
19 import eu.etaxonomy.cdm.model.metadata.CdmMetaData.MetaDataPropertyName;
20 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
21
22 /**
23 * Base class representing a CDM remote source.
24 * This class handles all the configuration relating to the remoting aspect of
25 * a CDM source.
26 *
27 */
28 public class CdmRemoteSourceBase extends CdmSource implements ICdmRemoteSource {
29
30 protected static final String DEFAULT_NAME = "default";
31 protected static final String DEFAULT_SERVER = "127.0.0.1";
32 protected static final int DEFAULT_PORT = 8080;
33 protected static final String DEFAULT_CONTEXT_PATH = "";
34 protected static final NomenclaturalCode DEFAULT_NOMENCLATURAL_CODE = NomenclaturalCode.ICNAFP;
35 private String contextPath;
36 private String baseUrl;
37
38 private IMetadataService metadataService;
39
40 /**
41 * Constructs a CdmRemoteSourceBase object with default values.
42 *
43 */
44 protected CdmRemoteSourceBase() {
45 setName(DEFAULT_NAME);
46 setServer(DEFAULT_SERVER);
47 setPort(DEFAULT_PORT);
48 setContextPath(DEFAULT_CONTEXT_PATH);
49 setNomenclaturalCode(DEFAULT_NOMENCLATURAL_CODE);
50 }
51
52 /**
53 * Constructs a CdmRemoteSourceBase
54 *
55 * @param name
56 * @param server
57 * @param port
58 * @param contextPath
59 * @param nomenclaturalCode
60 */
61 public CdmRemoteSourceBase(String name, String server, int port, String contextPath, NomenclaturalCode nomenclaturalCode) {
62 setName(name);
63 setServer(server);
64 setPort(port);
65 setContextPath(contextPath);
66 setNomenclaturalCode(nomenclaturalCode);
67 metadataService = CdmApplicationRemoteConfiguration.getMetadataService(this);
68 }
69
70
71 /* (non-Javadoc)
72 * @see eu.etaxonomy.cdm.remote.ICdmRemoteSource#getBaseUrl()
73 */
74 @Override
75 public String getBaseUrl() {
76 return baseUrl;
77 }
78
79 /**
80 * Sets the base url for the http-invoker services as listed in
81 * httpInvokerServicesClients.xml.
82 * e.g. for 'http://127.0.0.1:8080/col/remoting/common.service', the
83 * base url would be 'http://127.0.0.1:8080/col'
84 *
85 * @param baseUrl
86 */
87 public void setBaseUrl(String baseUrl) {
88 this.baseUrl = baseUrl;
89 }
90
91 /* (non-Javadoc)
92 * @see eu.etaxonomy.cdm.remote.ICdmRemoteSource#getContextPath()
93 */
94 @Override
95 public String getContextPath() {
96 return contextPath;
97 }
98
99 /**
100 * Sets the context path.
101 * e.g. for 'http://127.0.0.1:8080/col/remoting/common.service', the
102 * context path would be 'col'
103 *
104 * @param contextPath
105 */
106 public void setContextPath(String contextPath) {
107 this.contextPath = contextPath;
108 }
109
110 /* (non-Javadoc)
111 * @see eu.etaxonomy.cdm.config.CdmSource#getDbSchemaVersion()
112 */
113 @Override
114 public String getDbSchemaVersion() throws CdmSourceException {
115 return metadataService.getDbSchemaVersion();
116
117 }
118
119 /* (non-Javadoc)
120 * @see eu.etaxonomy.cdm.config.CdmSource#isDbEmpty()
121 */
122 @Override
123 public boolean isDbEmpty() throws CdmSourceException {
124 return metadataService.isDbEmpty();
125
126 }
127
128 /* (non-Javadoc)
129 * @see eu.etaxonomy.cdm.config.CdmSource#checkConnection()
130 */
131 @Override
132 public boolean checkConnection() throws CdmSourceException {
133 // assuming that database service works implies
134 // the connection is up
135 // if no exception is thrown then we assume that the
136 // connection is up
137 try {
138 metadataService.getDbSchemaVersion();
139 } catch (RemoteAccessException e) {
140 throw new CdmSourceException("RemoteAccessException: " + e.getMessage());
141 }
142
143 return true;
144 }
145
146 /* (non-Javadoc)
147 * @see eu.etaxonomy.cdm.config.CdmSource#getConnectionMessage()
148 */
149 @Override
150 public String getConnectionMessage() {
151 return "Connecting to Remote CDM Instance " + getName() + ":" + getPort() + "/" + getContextPath();
152 }
153
154
155 @Override
156 public Map<MetaDataPropertyName, String> getMetaDataMap() throws CdmSourceException {
157 return metadataService.getCdmMetadataMap();
158 }
159
160
161 }