summaryrefslogtreecommitdiff
path: root/src/YalpServer/InitServer.java
blob: 3e77ea5ce3454c9b9cda342ff82e511f922a48f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/*
 * Copyright (c) 2006 Manuel Traut and Volker Dahnke
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Common Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v10.html
 *
 * Contributors: Manuel Traut and Volker Dahnke
 *
 */

package YalpServer;

import java.net.Inet4Address;
import java.net.UnknownHostException;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;

import java.util.ArrayList;
import java.util.Properties;

import java.security.*;

import org.omg.CosNaming.*;
import org.omg.CosNaming.NamingContextPackage.*;
import org.omg.CORBA.*;
import org.omg.PortableServer.*;
import org.omg.PortableServer.POA;

import YalpInterfaces.*;

/*
 * Class InitServer
 *
 * <em>Establishes DBConnection and creates VlcStreamer waits for
 *     further Instructions via Interfaces</em>
 *
 * @author Volker Dahnke / Manuel Traut
 *
 * @version 0.1 20-11-2005<br>
 *
 * @see Server
 *
 */

public class InitServer {

	private static ServerSettings settings = new ServerSettings();
	public static InputPluginHandler inputHandler = new InputPluginHandler();

	private ServerControlImpl srvCon;
	private ServerControlInterface srv;
	private ORB orb;
	private String[] orbArgs;
	private POA poa;

	private String serverIP;

	/*
	 *  Constructor: starts Server initialization
	 */
	public InitServer(String[] _orbArgs) {

		loadConfig("ServerSettings.xml");
		writeConfig("ServerSettings.xml");

		this.orbArgs = _orbArgs; // t.b.d. read orbargs from config xml

		try {
			this.serverIP = Inet4Address.getLocalHost().getHostAddress();
		} catch(UnknownHostException e) {
			/* t.b.d. error handling */
			System.out.println("couldn't resolve hostname");
		}

		/* bind ServerControl to ORB and NamingService */
		this.orb = ORB.init(orbArgs, null);

		try {
			this.poa = POAHelper.narrow(orb.resolve_initial_references("RootPOA"));
		} catch(org.omg.CORBA.ORBPackage.InvalidName e) {
			/* t.b.d. error handling */
			System.out.println("couldn't get name ref of root poa");
		}

		try {
			poa.the_POAManager().activate();
		} catch(org.omg.PortableServer.POAManagerPackage.AdapterInactive e) {
			/* t.b.d. error handling */
			System.out.println("poa inactive");
		}

		srvCon = new ServerControlImpl();
		srvCon.setORB(orb);
		srvCon.init(this);

		try {
			org.omg.CORBA.Object ref = poa.servant_to_reference(srvCon);
			srv = ServerControlInterfaceHelper.narrow(ref);

			org.omg.CORBA.Object objRef =
				orb.resolve_initial_references("NameService");

			NamingContextExt ncRef = NamingContextExtHelper.narrow(objRef);
			String name = "YALP_Server";
			NameComponent path[] = ncRef.to_name(name);
			ncRef.rebind(path, srv);

			System.out.println("YALP Server ready");
			orb.run();

		} catch( org.omg.CosNaming.NamingContextPackage.InvalidName e) {
			/* t.b.d. error handling */
			System.out.println("couldn't narrow ref to path");
		} catch( org.omg.CosNaming.NamingContextPackage.NotFound e) {
			/* t.b.d. error handling */
			System.out.println("naming context not found, couldn't bind server ctl");
		} catch(org.omg.PortableServer.POAPackage.ServantNotActive e) {
			/* t.b.d. error handling */
			System.out.println("couldn't get name ref of root poa");
		} catch (org.omg.CORBA.ORBPackage.InvalidName e) {
			/* t.b.d. error handling */
			System.out.println("couldn't get NameService");
		} catch (org.omg.PortableServer.POAPackage.WrongPolicy e) {
			/* t.b.d. error handling */
			System.out.println("policies not set correctly");
		} catch (org.omg.CosNaming.NamingContextPackage.CannotProceed e) {
			/* t.b.d. error handling */
			System.out.println("rebind failed, cannot proceed");
		}
	}

/*
 * write Configuration to XML File
 *
 * @param fileName
 * 		 where to write the configuration file
 * @return boolean
 * 		 false - if failed
 */

	public boolean writeConfig(String fileName) {
		try{
			FileOutputStream configFile = new FileOutputStream(fileName);
			XMLEncoder configWriter = new XMLEncoder(configFile);
			configWriter.writeObject(settings.imageDir);
			configWriter.writeObject(settings.videoDir);
			configWriter.writeObject(settings.soundDir);
			configWriter.close();
		} catch (FileNotFoundException fnfe){
			return false;
		}
		return true;
	}

/*
 * tries to load ServerSettings from XML File
 * @param fileName
 * 		 Configuration file
 * @return boolean
 * 		 false - if loading failed
 */

	public boolean loadConfig(String fileName) {
		try{
			FileInputStream configFile = new FileInputStream(fileName);
			XMLDecoder configLoader = new XMLDecoder(configFile);
			settings.imageDir = (String)configLoader.readObject();
			settings.videoDir = (String)configLoader.readObject();
			settings.soundDir = (String)configLoader.readObject();
			configLoader.close();
		} catch(FileNotFoundException fnfe) {
			System.out.println("Configuration not found, loading defaults...");
			return false;
		} catch(ClassCastException cce) {
			System.out.println("Errors in Configuration, loading defaults...");
			return false;
		}
		return true;
	}

/*
 * returns actual configuration of the server
 * @return ServerSettings
 * 		 actual configuration
 */

	public ServerSettings getConfig() {
		return this.settings;
	}

/*
 * sets and saves a new ServerConfiguration
 *
 * @param set
 * 		 new ServerSettings
 * @return boolean
 * 		 true if succesfully saved
 */

	public boolean setConfig(ServerSettings set){
		this.settings = set;
		return this.writeConfig("ServerConfiguration.xml");
	}
}