/*
* 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
*
* Establishes DBConnection and creates VlcStreamer waits for
* further Instructions via Interfaces
*
* @author Volker Dahnke / Manuel Traut
*
* @version 0.1 20-11-2005
*
* @see Server
*
*/
public class InitServer {
private static ServerSettings settings = new ServerSettings();
public static InputPluginHandler inputHandler = new InputPluginHandler();
public static OutputPluginHandler outputHandler = new OutputPluginHandler();
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");
}
System.out.println("host: " + serverIP);
/* 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");
}
}