2 * //******************************************************************
4 * // Copyright 2016 Samsung Electronics All Rights Reserved.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * // http://www.apache.org/licenses/LICENSE-2.0
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.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.cloud.accountserver;
24 import java.net.InetSocketAddress;
25 import java.util.Scanner;
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;
44 * This class is in charge of running of account server.
47 public class AccountServer {
49 private static int coapServerPort;
50 private static boolean tlsMode;
51 private static String databaseHost;
52 private static String webLogHost;
54 public static void main(String[] args) throws Exception {
55 System.out.println("-----Account SERVER-----");
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");
64 if (webLogHost != null)
65 Log.InitWebLog(webLogHost,
66 AccountServer.class.getSimpleName().toString());
68 AccountDBManager.createInstance(databaseHost);
70 ServerSystem serverSystem = new ServerSystem();
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());
84 serverSystem.addServer(
85 new CoapServer(new InetSocketAddress(coapServerPort)));
87 serverSystem.startSystem(tlsMode);
89 Scanner in = new Scanner(System.in, "UTF-8");
91 System.out.println("press 'q' to terminate");
93 while (!in.nextLine().equals("q"));
97 System.out.println("Terminating...");
99 serverSystem.stopSystem();
101 System.out.println("Terminated");
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];
114 // configuration provided by docker env
115 String tlsModeEnv = System.getenv("TLS_MODE");
116 if (tlsModeEnv != null) {
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;