Project

General

Profile

« Previous | Next » 

Revision 15a62a24

Added by Cherian Mathew over 8 years ago

#4073 Initial implementation of local cdm-server

View differences:

eu.etaxonomy.taxeditor.cdmlib/src/main/java/eu/etaxonomy/taxeditor/remoting/source/CdmServerInfo.java
28 28
import org.json.JSONObject;
29 29

  
30 30
import eu.etaxonomy.cdm.config.CdmSourceException;
31
import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
32
import eu.etaxonomy.cdm.database.ICdmDataSource;
31 33
import eu.etaxonomy.taxeditor.remoting.server.CDMServerException;
32 34

  
33 35
/**
......
48 50
    private final static String NAME_TEST = "edit-test";
49 51
    private final static String SERVER_TEST = "test.e-taxonomy.eu";
50 52

  
51
    private final static String SERVER_LOCALHOST = "localhost";
53
    public final static String SERVER_LOCALHOST = "localhost";
52 54
    private final static String NAME_LOCALHOST = "localhost";
53
    private final static String NAME_LOCALHOST_MGD = "localhost mgd.";
55
    public final static String NAME_LOCALHOST_MGD = "localhost mgd.";
54 56

  
55 57
    private final static String NAME_LOCALHOST_DEV = "localhost-dev";
56 58
    private final static String NAME_INSTANCE_LOCALHOST_DEV = "local-dev";
......
63 65
    private final int port;
64 66
    private final List<CdmInstanceInfo> instances;
65 67

  
68
    private static List<CdmServerInfo> cdmServerInfoList;
66 69

  
67 70
    public CdmServerInfo(String name, String server, int port) {
68 71
        this.name = name;
......
80 83
    }
81 84

  
82 85
    public boolean isLocalhost() {
83
        return name.startsWith(SERVER_LOCALHOST_DEV);
86
        return name.startsWith(SERVER_LOCALHOST);
84 87
    }
88

  
89
    public boolean isLocalhostMgd() {
90
        return NAME_LOCALHOST_MGD.equals(name);
91
    }
92

  
85 93
    public void refreshInstances() throws CDMServerException {
86 94
        instances.clear();
95
        if(isLocalhostMgd()) {
96
            addInstancesFromDataSourcesConfig();
97
        } else {
98
            addInstancesViaHttp();
99
        }
100

  
101
    }
102

  
103
    public void addInstancesViaHttp() throws CDMServerException {
87 104
        String url = "http://" + server + ":" + String.valueOf(port) + "/" + CDMSERVER_PREFIX + "/instances.jsp";
88 105

  
89 106
        HttpClient client = new DefaultHttpClient();
......
138 155
                throw new CDMServerException(e);
139 156
            }
140 157
        }
158
    }
159

  
160
    public void addInstancesFromDataSourcesConfig() {
161

  
162
        for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()){
163
            logger.warn("Adding local instance " + dataSource.getName());
164
            addInstance(dataSource.getName(), dataSource.getName());
165
        }
141 166

  
142 167
    }
143 168

  
......
196 221
    }
197 222

  
198 223
    public static List<CdmServerInfo> getCdmServers() {
199
        List<CdmServerInfo> cdmServerInfoList = new ArrayList<CdmServerInfo>();
200
        cdmServerInfoList.add(new CdmServerInfo(NAME_PRODUCTION, SERVER_PRODUCTION, 80));
201
        cdmServerInfoList.add(new CdmServerInfo(NAME_INTEGRATION, SERVER_INTEGRATION, 80));
202
        cdmServerInfoList.add(new CdmServerInfo(NAME_TEST, SERVER_TEST, 80));
203
        cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST, SERVER_LOCALHOST, 8080));
204
        cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST_MGD, SERVER_LOCALHOST,8080));
224
        if(cdmServerInfoList == null) {
225
            cdmServerInfoList = new ArrayList<CdmServerInfo>();
226
            cdmServerInfoList.add(new CdmServerInfo(NAME_PRODUCTION, SERVER_PRODUCTION, 80));
227
            cdmServerInfoList.add(new CdmServerInfo(NAME_INTEGRATION, SERVER_INTEGRATION, 80));
228
            cdmServerInfoList.add(new CdmServerInfo(NAME_TEST, SERVER_TEST, 80));
229
            cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST, SERVER_LOCALHOST, 8080));
230
            cdmServerInfoList.add(new CdmServerInfo(NAME_LOCALHOST_MGD, SERVER_LOCALHOST,8080));
231
        }
205 232
        return cdmServerInfoList;
206 233
    }
207 234

  
......
248 275

  
249 276
    public class CdmInstanceInfo {
250 277
        private final String name;
278

  
279
        /**
280
         * The full path of the instance including the the prefix (if any).
281
         * E.g. for an EDIT instance this would be something like "cdmserver/remoting"
282
         * For a managed local server this would simply be "remoting"
283
         */
251 284
        private final String basePath;
252 285

  
253 286

  
eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/TaxonEditorInput.java
10 10
package eu.etaxonomy.taxeditor.editor;
11 11

  
12 12
import java.util.Arrays;
13
import java.util.HashMap;
13 14
import java.util.HashSet;
14 15
import java.util.List;
15 16
import java.util.Map;
......
27 28
import eu.etaxonomy.cdm.api.service.ITaxonNodeService;
28 29
import eu.etaxonomy.cdm.api.service.ITaxonService;
29 30
import eu.etaxonomy.cdm.model.common.CdmBase;
31
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
30 32
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
31 33
import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
32 34
import eu.etaxonomy.cdm.model.taxon.Synonym;
......
102 104
                "taxon.rights",
103 105
                "taxon.sources",
104 106
                "taxon.descriptions",
105
                "taxon.synonymRelations",
107
                "taxon.synonymRelations.relatedFrom.name.homotypicalGroup.typifiedNames",
106 108
                "taxon.relationsToThisTaxon",
107 109
                "taxon.relationsFromThisTaxon",
108 110
                "taxon.taxonNodes",
......
126 128

  
127 129
    private void initForTaxonBase(UUID taxonBaseUuid) {
128 130

  
129
        TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid);
131

  
132
        List<String> taxonBasePropertyPaths = Arrays.asList(new String[] {
133
                "annotations",
134
                "markers",
135
                "credits",
136
                "extensions",
137
                "rights",
138
                "sources",
139
                "descriptions",
140
                "relationsToThisTaxon",
141
                "relationsFromThisTaxon",
142
                "taxonNodes",
143
                "name.status",
144
                "name.nomenclaturalReference",
145
                "synonymRelations.relatedFrom.name.status.type",
146
                "synonymRelations.relatedFrom.name.nomenclaturalReference.inReference",
147
                "name.taxonBases",
148
                "name.descriptions.descriptionElements",
149
                "name.descriptions.markers"
150

  
151

  
152
        });
153
        TaxonBase taxonBase = CdmStore.getService(ITaxonService.class).load(taxonBaseUuid, taxonBasePropertyPaths);
130 154
        if (taxonBase != null){
131 155
            if(taxonBase.isInstanceOf(Taxon.class)){
132 156
                Taxon taxon = CdmBase.deproxy(taxonBase, Taxon.class);
......
476 500

  
477 501
    @Override
478 502
    public Map<Object, List<String>> getPropertyPathsMap() {
479
//        Map<Object, List<String>> propertyPathsMap = new HashMap<Object, List<String>>();
503
        Map<Object, List<String>> propertyPathsMap = new HashMap<Object, List<String>>();
480 504
//        List<String> taxonNameBasePropertyPaths = Arrays.asList(new String[] {
481 505
//                "status",
482 506
//                "taxonBases.taxonNodes",
......
497 521
//                 "taxon.descriptions"
498 522
//         });
499 523
//        propertyPathsMap.put(TaxonNode.class, taxonBasePropertyPaths);
500
//        return propertyPathsMap;
501
        return null;
524
       List<String> homotypicalGroupPropertyPaths = Arrays.asList(new String[] {
525
                "typifiedNames.taxonBases.synonymRelations.relatedFrom.name",
526
                "typifiedNames.taxonBases.synonymRelations.relatedFrom.name.status"
527
        });
528
       propertyPathsMap.put(HomotypicalGroup.class, homotypicalGroupPropertyPaths);
529
       return propertyPathsMap;
530
//        return null;
502 531
    }
503 532

  
504 533
}
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/ui/dialog/RemotingLoginDialog.java
169 169

  
170 170
        xpndblcmpstAdvanced.setExpanded(false);
171 171

  
172
        CdmRemoteSource devRemoteSource = CdmServerInfo.getDevServerRemoteSource();
173
        if(devRemoteSource != null) {
174
            CdmStore.connect(devRemoteSource, this);
175
            isDevRemoteSource = true;
176
        }
177

  
178 172
        Display display = getParent().getDisplay();
179 173

  
180 174
        while (!shlConnect.isDisposed()) {
......
462 456
                        }
463 457
                        refreshCdmServer();
464 458
                        updatePort();
459
                        CdmRemoteSource devRemoteSource = CdmServerInfo.getDevServerRemoteSource();
460
                        if(devRemoteSource != null) {
461
                            String username = System.getProperty("cdm.server.dev.username");
462
                            String password = System.getProperty("cdm.server.dev.password");
463
                            if(username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
464
                                txtLogin.setText(username);
465
                                txtPassword.setText(password);
466
                                CdmStore.connect(devRemoteSource, RemotingLoginDialog.this);
467
                            }
468
                        }
465 469
                    }
466 470
                });
467 471
                return Status.OK_STATUS;
......
683 687

  
684 688
    private void readPrefCredentials() {
685 689
        String username, password;
686
        if(isDevRemoteSource) {
687
            username = System.getProperty("cdm.server.dev.username");
688
            password = System.getProperty("cdm.server.dev.password");
689
            if(username != null && !username.isEmpty() && password != null && !password.isEmpty()) {
690
                txtLogin.setText(username);
691
                txtPassword.setText(password);
692
                return;
693
            }
694
        }
695 690
        IEclipsePreferences preferences = ConfigurationScope.INSTANCE.getNode(STORE_PREFERENCES_NODE);
696 691
        Preferences credentialsPrefs = preferences.node(LOGIN_NODE);
697 692
        username = credentialsPrefs.get(getUsernamePrefKey(), "");
......
713 708

  
714 709
    private void emptyCredentials() {
715 710
        txtLogin.setText("");
716
        txtLogin.setText("");
711
        txtPassword.setText("");
717 712
    }
718 713

  
719 714
    private String getUsernamePrefKey() {
eu.etaxonomy.taxeditor.test/META-INF/MANIFEST.MF
30 30
 lib/unitils-dbmaintainer-3.4.2.jar,
31 31
 lib/unitils-dbunit-3.4.2.jar,
32 32
 lib/unitils-spring-3.4.2.jar,
33
 lib/dbunit-2.4.9.jar
34
Export-Package: org.hamcrest
33
 lib/dbunit-2.4.9.jar
eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/BaseRemotingTest.java
21 21
import org.apache.log4j.Logger;
22 22
import org.eclipse.core.runtime.FileLocator;
23 23
import org.eclipse.core.runtime.Platform;
24
import org.junit.AfterClass;
25 24
import org.junit.Assert;
26 25
import org.junit.BeforeClass;
27 26
import org.osgi.framework.Bundle;
......
68 67
    public static final Resource SERVER_PROPERTIES_FILE =
69 68
            new ClassPathResource("server.properties");
70 69

  
71
//    public static final Resource EDITOR_DATASOURCES_FILE =
72
//            new ClassPathResource(".cdmLibrary/writableResources/cdm.datasources.xml");
73

  
74
    //private static CdmApplicationRemoteController remoteApplicationController;
75 70
    private static ICdmRemoteSource cdmRemoteSource;
76 71
    private static CdmPersistentRemoteSource remotePersistentSource;
77 72

  
......
88 83
    private static String user = DEFAULT_USER;
89 84
    private static String password = DEFAULT_PASSWORD;
90 85

  
86
    protected static CDMServer cdmServer;
87

  
91 88
    @BeforeClass
92 89
    public static void initializeBaseRemotingTest() {
93 90

  
......
101 98
            File userHomeDir = new File(FileLocator.resolve(userHomeDirURL).toURI());
102 99
            userHomeDirPath = userHomeDir.getAbsolutePath();
103 100

  
104

  
105

  
106
            logger.info("Setting user.home to " + userHomeDirPath);
107
            System.setProperty(userHomeKey, userHomeDirPath);
108

  
109
            CDMServer cdmServer = CDMServer.getInstance();
110

  
111

  
112
            Properties prop = new Properties();
113

  
114 101
            URL serverPropertiesURL = bundle.getEntry("src/test/resources/server.properties");
115 102
            File serverPropertiesFile = new File(FileLocator.resolve(serverPropertiesURL).toURI());
116 103
            InputStream inputStream = new FileInputStream(serverPropertiesFile);
117 104

  
105
            Properties prop = new Properties();
118 106
            if (inputStream != null) {
119 107
                prop.load(inputStream);
120 108
                inputStream.close();
121 109
            }
122 110

  
123
            if(prop.getProperty("httpPort") != null) {
124
                cdmServer.setHttpPort(Integer.valueOf(prop.getProperty("httpPort")));
125
            }
111
            logger.info("Setting user.home to " + userHomeDirPath);
112
            System.setProperty(userHomeKey, userHomeDirPath);
126 113

  
127
            if(prop.getProperty("stopPort") != null) {
128
                cdmServer.setStopPort(Integer.valueOf(prop.getProperty("stopPort")));
129
            }
114
            cdmServer = new CDMServer("cdmTest");
130 115

  
131
            if(prop.getProperty("stopKey") != null) {
132
                cdmServer.setStopKey(prop.getProperty("stopKey"));
133
            }
134 116

  
135 117
            if(prop.getProperty("user") != null) {
136 118
                user = prop.getProperty("user");
......
140 122
                password = prop.getProperty("password");
141 123
            }
142 124

  
143
            cdmServer.start();
144
            initializeController(CDMServer.getInstance().getName(),
145
                    CDMServer.getInstance().getHost(),
146
                    CDMServer.getInstance().getPort(),
147
                    CDMServer.getInstance().getContextPath(),
125
            initializeController(cdmServer.getName(),
126
                    cdmServer.getHost(),
127
                    cdmServer.getPort(),
128
                    cdmServer.getContextPath(),
148 129
                    NomenclaturalCode.ICNAFP,
149 130
                    user,
150 131
                    password);
......
234 215
        return (CdmTransientEntityCacher) getFieldValueViaReflection(getSession(sessionOwner), "cdmTransientEntityCacher");
235 216
    }
236 217

  
237
    @AfterClass
238
    public static void cleanup() {
239
        try {
240
            CDMServer.getInstance().stop();
241
        } catch (Exception e) {
242
            Assert.fail("Server could not be stopped. Reason : " + e.getMessage());
243
        }
244
    }
245

  
246 218

  
247 219
    protected static Object getFieldValueViaReflection(Object object, String fieldName) {
248 220
        Class<?> clazz = object.getClass();
eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/CDMServer.java
10 10
package eu.etaxonomy.taxeditor.httpinvoker;
11 11

  
12 12
import java.io.BufferedReader;
13
import java.io.File;
14
import java.io.FileInputStream;
15
import java.io.FileNotFoundException;
13 16
import java.io.IOException;
17
import java.io.InputStream;
14 18
import java.io.InputStreamReader;
19
import java.net.URISyntaxException;
20
import java.net.URL;
21
import java.util.Properties;
15 22

  
16 23
import javax.management.InstanceNotFoundException;
17 24
import javax.management.MBeanException;
......
25 32
import javax.sql.DataSource;
26 33

  
27 34
import org.apache.log4j.Logger;
35
import org.eclipse.core.runtime.FileLocator;
36
import org.eclipse.core.runtime.Platform;
37
import org.osgi.framework.Bundle;
28 38
import org.springframework.core.io.ClassPathResource;
29 39
import org.springframework.core.io.Resource;
30 40
import org.unitils.database.annotations.TestDataSource;
31 41

  
42
import eu.etaxonomy.cdm.database.CdmPersistentDataSource;
43
import eu.etaxonomy.cdm.database.ICdmDataSource;
32 44
import eu.etaxonomy.taxeditor.remoting.server.CDMServerException;
33 45
import eu.etaxonomy.taxeditor.remoting.source.CdmRemoteSourceBase;
34 46

  
......
77 89

  
78 90
    private boolean serverAlreadyRunning = false;
79 91

  
80
    private void CDMServer() {}
92
    private File dataSourcesFile;
93
    private final String dataSourceName;
81 94

  
82
    public static CDMServer getInstance() {
83
        if(cdmServer == null) {
84
            cdmServer = new CDMServer();
95
    public CDMServer(String dataSourceName) throws CDMServerException {
96
        this.dataSourceName = dataSourceName;
97
        Properties prop = new Properties();
98

  
99
        Bundle bundle = Platform.getBundle("eu.etaxonomy.taxeditor.cdmlib");
100
        URL serverPropertiesURL = bundle.getEntry("src/test/resources/server.properties");
101

  
102
        try {
103
            File serverPropertiesFile = new File(FileLocator.resolve(serverPropertiesURL).toURI());
104
            InputStream inputStream = new FileInputStream(serverPropertiesFile);
105

  
106
            if (inputStream != null) {
107
                prop.load(inputStream);
108
                inputStream.close();
109
            }
110
        } catch (FileNotFoundException e) {
111
            throw new CDMServerException(e);
112
        } catch (URISyntaxException e) {
113
            throw new CDMServerException(e);
114
        } catch (IOException e) {
115
            throw new CDMServerException(e);
85 116
        }
86
        return cdmServer;
117

  
118

  
119

  
120
        if(prop.getProperty("httpPort") != null) {
121
            setHttpPort(Integer.valueOf(prop.getProperty("httpPort")));
122
        }
123

  
124
        if(prop.getProperty("stopPort") != null) {
125
            setStopPort(Integer.valueOf(prop.getProperty("stopPort")));
126
        }
127

  
128
        if(prop.getProperty("stopKey") != null) {
129
            setStopKey(prop.getProperty("stopKey"));
130
        }
131

  
87 132
    }
88 133

  
134

  
135

  
89 136
    public String getName() {
90 137
        return name;
91 138
    }
......
265 312
    public void stop(boolean force) throws Exception {
266 313

  
267 314
        if(!force) {
268
            if(!getInstance().isStarted(1)) {
315
            if(!cdmServer.isStarted(1)) {
269 316
                logger.info("[CDM-Server] Server already stopped @ " + host + ":" + httpPort );
270 317
                return;
271 318
            }
......
335 382
        } catch (MalformedObjectNameException e) {
336 383
            throw new CDMServerException(e);
337 384
        }
385
    }
338 386

  
387
    public void convertEditorToServerConfig() {
388
        String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> " + System.lineSeparator() +
389
                "<beans xmlns=\"http://www.springframework.org/schema/beans\"" + System.lineSeparator() +
390
                "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" + System.lineSeparator() +
391
                "xmlns:tx=\"http://www.springframework.org/schema/tx\"" + System.lineSeparator() +
392
                "xmlns:context=\"http://www.springframework.org/schema/context\"" + System.lineSeparator() +
393
                "xsi:schemaLocation=\"http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" + System.lineSeparator() +
394
                "http://www.springframework.org/schema/context   http://www.springframework.org/schema/context/spring-context-2.5.xsd" + System.lineSeparator() +
395
                "http://www.springframework.org/schema/tx   http://www.springframework.org/schema/tx/spring-tx-2.5.xsd" + System.lineSeparator() +
396
                ">" + System.lineSeparator() +
397
                "<bean id=\"dataSourceProperties\" class=\"eu.etaxonomy.cdm.remote.config.DataSourceProperties\">" + System.lineSeparator() +
398
                "   <property name=\"propsMap\">" + System.lineSeparator() +
399
                "       <map/>" + System.lineSeparator() +
400
                "   </property>" + System.lineSeparator() +
401
                "</bean>";
402

  
403
        for(ICdmDataSource dataSource : CdmPersistentDataSource.getAllDataSources()) {
339 404

  
340

  
405
        }
341 406
    }
342 407
}
eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/CdmRemoteSourceTest.java
24 24

  
25 25
	@Test
26 26
	public void whenConnectingToInactiveServerThenFailToConnect() {
27

  
27 28
		// check if non-active server throws the right exception
28
		CdmRemoteSource inactiveCrs = CdmRemoteSource.NewInstance(CDMServer.getInstance().getName(),
29
		        CDMServer.getInstance().getHost(),
29
		CdmRemoteSource inactiveCrs = CdmRemoteSource.NewInstance(cdmServer.getName(),
30
		        cdmServer.getHost(),
30 31
		        808080,
31
		        CDMServer.getInstance().getContextPath(),
32
		        cdmServer.getContextPath(),
32 33
		        NomenclaturalCode.ICNAFP);
33 34
		try {
34 35
			inactiveCrs.getDbSchemaVersion();
......
67 68
	@Test
68 69
	public void whenConnectingToAnActiveServerThenConnectSuccessfully() {
69 70
		// check if active server throws the right exception
70
		CdmRemoteSource activeCrs = CdmRemoteSource.NewInstance(CDMServer.getInstance().getName(),
71
                CDMServer.getInstance().getHost(),
72
                CDMServer.getInstance().getPort(),
73
                CDMServer.getInstance().getContextPath(),
71
		CdmRemoteSource activeCrs = CdmRemoteSource.NewInstance(cdmServer.getName(),
72
		        cdmServer.getHost(),
73
		        cdmServer.getPort(),
74
		        cdmServer.getContextPath(),
74 75
                NomenclaturalCode.ICNAFP);
75 76
		String dbSchemaVersion = "";
76 77
		try {
eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/httpinvoker/CdmServerTest.java
27 27

  
28 28
    @Test
29 29
    public void startCdmServer() throws CDMServerException {
30
        CDMServer.getInstance().start();
31
    }
32

  
33
    @Test
34
    public void stopCdmServer() {
30
        CDMServer cdmServer = new CDMServer("cdmTest");
31
        cdmServer .start();
35 32
        try {
36
            CDMServer.getInstance().stop(true);
33
            cdmServer.stop(true);
37 34
        } catch (Exception e) {
38 35
            e.printStackTrace();
39 36
            Assert.fail("Server could not be stopped. Reason : " + e.getMessage());
eu.etaxonomy.taxeditor.test/src/test/java/eu/etaxonomy/taxeditor/ui/dialogs/CdmServerInfoTest.java
64 64

  
65 65
        }
66 66
    }
67

  
68
    @Test
69
    public void refreshInstancesTest() throws CDMServerException {
70
        CdmServerInfo cdmServerInfo = new CdmServerInfo(CdmServerInfo.NAME_LOCALHOST_MGD, CdmServerInfo.SERVER_LOCALHOST,8080);
71
        cdmServerInfo.refreshInstances();
72
        List<CdmInstanceInfo> instances = cdmServerInfo.getInstances();
73
        Assert.assertTrue(instances != null && !instances.isEmpty());
74
    }
75

  
76
    @Test
77
    public void convertToServerConfigTest() {
78

  
79

  
80
    }
67 81
}

Also available in: Unified diff