8e146607cfd5e5450e17d1751c185b5c6c266c85
[iotivity.git] / cloud / account / src / main / java / org / iotivity / cloud / accountserver / AccountServer.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2016 Samsung Electronics All Rights Reserved.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
8  * // Licensed under the Apache License, Version 2.0 (the "License");
9  * // you may not use this file except in compliance with the License.
10  * // You may obtain a copy of the License at
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
14  * // Unless required by applicable law or agreed to in writing, software
15  * // distributed under the License is distributed on an "AS IS" BASIS,
16  * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * // See the License for the specific language governing permissions and
18  * // limitations under the License.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.accountserver;
23
24 import java.net.InetSocketAddress;
25 import java.util.Scanner;
26
27 import org.iotivity.cloud.accountserver.db.AccountDBManager;
28 import org.iotivity.cloud.accountserver.resources.account.AccountResource;
29 import org.iotivity.cloud.accountserver.resources.account.session.SessionResource;
30 import org.iotivity.cloud.accountserver.resources.account.tokenrefresh.TokenRefreshResource;
31 import org.iotivity.cloud.accountserver.resources.acl.group.GroupResource;
32 import org.iotivity.cloud.accountserver.resources.acl.id.AclResource;
33 import org.iotivity.cloud.accountserver.resources.acl.invite.InviteResource;
34 import org.iotivity.cloud.accountserver.resources.acl.verify.AclVerifyResource;
35 import org.iotivity.cloud.accountserver.resources.credprov.cert.CertificateResource;
36 import org.iotivity.cloud.accountserver.resources.credprov.crl.CrlResource;
37 import org.iotivity.cloud.base.ServerSystem;
38 import org.iotivity.cloud.base.resource.CloudPingResource;
39 import org.iotivity.cloud.base.server.CoapServer;
40 import org.iotivity.cloud.util.Log;
41
42 /**
43  *
44  * This class is in charge of running of account server.
45  *
46  */
47 public class AccountServer {
48
49     private static int     coapServerPort;
50     private static boolean tlsMode;
51     private static String  databaseHost;
52     private static String  webLogHost;
53
54     public static void main(String[] args) throws Exception {
55         System.out.println("-----Account SERVER-----");
56         Log.Init();
57
58         if (!parseConfiguration(args)) {
59             Log.e("\nCoAP-server <Port> Database <Address> <Port> TLS-mode <0|1> are required. WebSocketLog-Server <Addres> <Port> is optional.\n"
60                     + "ex) " + Constants.DEFAULT_COAP_PORT
61                     + " 127.0.0.1 27017 0\n");
62             return;
63         }
64         if (webLogHost != null)
65             Log.InitWebLog(webLogHost,
66                     AccountServer.class.getSimpleName().toString());
67
68         AccountDBManager.createInstance(databaseHost);
69
70         ServerSystem serverSystem = new ServerSystem();
71
72         serverSystem.addResource(new CloudPingResource());
73         serverSystem.addResource(new AccountResource());
74         serverSystem.addResource(new SessionResource());
75         serverSystem.addResource(new TokenRefreshResource());
76         serverSystem.addResource(new GroupResource());
77         serverSystem.addResource(new AclResource());
78         serverSystem.addResource(new AclVerifyResource());
79         serverSystem.addResource(new CertificateResource());
80         serverSystem.addResource(new CrlResource());
81         serverSystem.addResource(new AclResource());
82         serverSystem.addResource(new InviteResource());
83
84         serverSystem.addServer(
85                 new CoapServer(new InetSocketAddress(coapServerPort)));
86
87         serverSystem.startSystem(tlsMode);
88
89         Scanner in = new Scanner(System.in, "UTF-8");
90
91         System.out.println("press 'q' to terminate");
92
93         while (!in.nextLine().equals("q"));
94
95         in.close();
96
97         System.out.println("Terminating...");
98
99         serverSystem.stopSystem();
100
101         System.out.println("Terminated");
102     }
103
104     private static boolean parseConfiguration(String[] args) {
105         // configuration provided by arguments
106         if (args.length == 4 || args.length == 6) {
107             coapServerPort = Integer.parseInt(args[0]);
108             databaseHost = args[1] + ":" + args[2];
109             tlsMode = Integer.parseInt(args[3]) == 1;
110             if (args.length == 6)
111                 webLogHost = args[4] + ":" + args[5];
112             return true;
113         }
114         // configuration provided by docker env
115         String tlsModeEnv = System.getenv("TLS_MODE");
116         if (tlsModeEnv != null) {
117
118             coapServerPort = Integer.parseInt(System.getenv("COAP_PORT"));
119             databaseHost = System.getenv("MONGODB_ADDRESS") + ":"
120                     + System.getenv("MONGODB_PORT");
121             tlsMode = Integer.parseInt(tlsModeEnv) == 1;
122             return true;
123         }
124         return false;
125     }
126 }