#! /bin/bash
. acsstartupAcsPorts
. acsstartupLogging.sh
. acsstartupConstants
. acsstartupAcsInstance
#*******************************************************************************
# ALMA - Atacama Large Millimiter Array
# (c) Associated Universities Inc., 2002 
# (c) European Southern Observatory, 2002
# Copyright by ESO (in the framework of the ALMA collaboration)
# and Cosylab 2002, All rights reserved
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, 
# MA 02111-1307  USA
#
# "@@(#) $Id: maciManagerJ,v 1.52.2.1 2009/12/20 21:46:49 hsommer Exp $"
#
# who       when      what
# -------- ---------- ----------------------------------------------
# msekoran 2003-05-09 Created.
#

#************************************************************************
#   NAME	maciManagerJ
#
#   SYNOPSIS
#
#   DESCRIPTION
#
#   FILES
#
#   ENVIRONMENT
#
#   RETURN VALUES
#
#   CAUTIONS
#
#   EXAMPLES
#
#   SEE ALSO
#
#   BUGS
#
#------------------------------------------------------------------------
#
#

INSTANCE_DIR=`getInstanceDirName $ACS_INSTANCE`
MANAGER_PIDFILE=$INSTANCE_DIR/$ACS_MANAGER_PIDFILE

#
# Save the process ID
#
ACS_MANAGER_PID=$$
if [ ! -d $INSTANCE_DIR ]
then
    ACS_LOG_ERROR "Unable to save manager's PID because '$INSTANCE_DIR' does not exist!"

elif [ ! -w $INSTANCE_DIR ]
then
    ACS_LOG_ERROR "Unable to save manager's PID because '$INSTANCE_DIR' is not owned by '$USER'!"

else
    #save the PID of this script so we can kill it later if we have to
    echo $ACS_MANAGER_PID  > $MANAGER_PIDFILE
fi


# ----------------------------------------------------------------

ACS_LOG_INFO "Starting jManager"

#
# Fetches the IP adress
#
export HOST=`getIP`

#
# If $NAMESERVICE_REFERENCE is not set, take the localhost name  
#
if [ "X$NAMESERVICE_REFERENCE" = "X" ]
then  
    ACS_NAMING_SERVICE_PORT=`getNamingServicePort`    
    export NAMESERVICE_REFERENCE=iiop://$HOST:$ACS_NAMING_SERVICE_PORT
fi

#
# Fetches port numbers
#      
export ACS_MANAGER_PORT=`getManagerPort`
export ACS_CDB_PORT=`getCDBPort`

if ! checkTCPPort $ACS_MANAGER_PORT
then
    ACS_LOG_ERROR "Manager port is being used by another process. Cannot continue!"
    exit $EC_NOPORT
fi
                                                         
ACS_LOG_DEBUG "ACS Naming Service: $NAMESERVICE_REFERENCE"
ACS_LOG_DEBUG "Manager will be run on port: $ACS_MANAGER_PORT"

#
# Evaluates properties for Manager federation
# The variable ACS_FEDERATION will remain empy if no federation
# is requested.
#
ACS_FEDERATION=""
if [ "X$ACS_DOMAINS" != "X" ]
then  
     ACS_FEDERATION="$ACS_FEDERATION -DACS.domains=$ACS_DOMAINS"
fi
if [ "X$ACS_FEDERATION_DIRECTORY" != "X" ]
then  
     ACS_FEDERATION="$ACS_FEDERATION -DACS.federationDirectory=$ACS_FEDERATION_DIRECTORY"
fi

#
# Evaluates property for DAL reference
# The variable ACS_DAL_REF will be set
#
ACS_DAL_REF=""
if [ "X$DAL_REFERENCE" != "X" ]
then
     ACS_DAL_REF="$DAL_REFERENCE"
else
     ACS_DAL_REF="corbaloc::$HOST:$ACS_CDB_PORT/CDB"
fi

#
# Set JVM_GC_LOG_NAME so that acsStartJava will run the JVM printing garbage collector details to file "jmanager.gclog".
# @TODO: When using several federated manager instances, we'll have to ensure that the jmanager.gclog files don't interfere with each other!
#
export JVM_GC_LOG_NAME=jmanager

#
# With ACS 8.1, more demanding testing at the OSF exposed connection problems to the manager, 
# which  may be caused by too many open file and TCP connections. While investigating this, we go back to using a custom classpath,
# so that not all jar files from all subsystems compete with the manager's TCP connections for the limited number of handles.
# As a first step, I put all ACS jar files except for the most obviously not needed ones on the classpath. 
# Later we need to remove some of them, taking into account that CERN alarm stuff is loaded via classForName. 
#
acsStartJava -endorsed \
    -noClassLoader -noAutoClasspath -addToClasspath abeansR2Components.jar:AbsoluteLayout.jar:acsAlarmClients.jar:acsalarmSchemaBindings.jar:acsAlarmService.jar:AcsAlarmSystem.jar:acsASsources.jar:acsCallbacksSupport.jar:acscodegen.jar:acscommon.jar:acscomponent.jar:ACScomponentcontLogTestComp.jar:ACScomponentcounterConsumerComp.jar:ACScomponentcounterSupplierComp.jar:ACScomponentjcontexmplComp.jar:acsContainerServices.jar:acsdaemon.jar:acsdaemonErrType.jar:acserr.jar:acserrHandlersErr.jar:acserrj.jar:ACSError.jar:ACSErrTypeAlarm.jar:acsErrTypeAlarmSourceFactory.jar:ACSErrTypeCommon.jar:acsErrTypeComponent.jar:acsErrTypeContainerServices.jar:ACSErrTypeCORBA.jar:ACSErrTypeCppNative.jar:ACSErrTypeDevIO.jar:ACSErrTypeJavaNative.jar:acsErrTypeLifeCycle.jar:ACSErrTypeMonitor.jar:ACSErrTypeOK.jar:ACSErrTypePythonNative.jar:acsGUIutil.jar:ACSIRSentinel.jar:acsjlog.jar:acsjlogSLF4j.jar:acsjms.jar:ACSJMSMessageEntity.jar:acslog.jar:acsnc.jar:acsncErrType.jar:acsncSchemaBindings.jar:AcsNCTraceLogLTS.jar:acspluginbuilder.jar:acsQoSErrType.jar:acssamp.jar:acstestcomp.jar:acsTestEntities.jar:acsthreadErrType.jar:acstime.jar:ACSTimeError.jar:activation.jar:ahclib-1.2.jar:alarmPanel.jar:alarmSourceGui.jar:AlarmSystem.jar:alarmsysteminterface.jar:antlr.jar:archive_xmlstore_if.jar:ArchiveIdentifierError.jar:asdemo.jar:avalon-framework.jar:baci.jar:baciErrTypeDevIO.jar:baciErrTypeProperty.jar:boot.jar:bulkData.jar:bulkDataDistributer.jar:bulkDataReceiver.jar:bulkDataSender.jar:castor.jar:CDB.jar:CDBBrowser.jar:cdbChecker.jar:cdbDAL.jar:cdbErrType.jar:cdbjDAL.jar:cdbSchemaBindings.jar:characteristicTask.jar:cmwmom.jar:commons-cli-1.0.jar:commons-collections-3.1.jar:commons-lang-2.1.jar:commons-logging.jar:commontypes.jar:comphelpgen.jar:concurrent.jar:contLogTest_IF.jar:contLogTestClient.jar:contNcTest_IF.jar:core-windows.jar:core.jar:demoComponents.jar:dom4j-1.6.1.jar:ebe.jar:ejb3-persistence.jar:endorsexercesImpl.jar:enumpropStd.jar:ErrorSystemComponent.jar:ErrorSystemErrType.jar:EventComponent.jar:gnu-regexp-1.1.4.jar:gp.jar:gpopenide.jar:hibernate-annotations.jar:hibernate-cglib-repack-2.1_3.jar:hibernate-commons-annotations.jar:hibernate3.jar:icons.jar:infobus.jar:jacstime.jar:jACSUtil.jar:jakarta-:oro-2.0.5.jar:jakarta-regexp-1.2.jar:java-getopt-1.0.12.jar:JavaContainerError.jar:JavaOnlyAcsConfig.jar:javassist-3.4.GA.jar:jaxen-core.jar:jaxen-jdom.jar:jcont.jar:jcontnc.jar:jdom.jar:jfits.jar:jfree-jcommon-1.0.10.jar:jfreechart-1.0.6.jar:jhall-2.0_02.jar:jlibeps-0.1.jar:jManager.jar:jmanagerErrType.jar:jms.jar:jnlp.jar:jta-1.1.jar:laserclient.jar:laserconsole.jar:lasercore.jar:laserdefinition.jar:laserguiplatform.jar:laserutil.jar:lc.jar:lcEngine.jar:log4j-1.2.8.jar:logging_idl.jar:logkit.jar:maci.jar:maciErrType.jar:maciSchemaBindings.jar:mastercomp_if.jar:NotificationServiceMC.jar:NotifyExt.jar:NotifyMonitoringExt.jar:openide-io.jar:openide.jar:openorb13Debug.jar:PatternAlarmCleared.jar:PatternAlarmTriggered.jar:prevayler-1.02.001.jar:repeatGuard.jar:samplingSystemUI.jar:saxpath.jar:selector-1.1.jar:Simulator.jar:slf4j-api-1.5.8.jar:systementities.jar:tablelayout.jar:typeSafeLogsLTS.jar:vtd-xml.jar:xalan.jar:xalan_serializer.jar:xmlentity.jar:xmljbind.jar:xtmodcoll-3.0.jar \
    -Djacorb.poa.thread_pool_max=200 \
    -DNamingServiceRemoteDirectory.reference=$NAMESERVICE_REFERENCE \
    -DDAL.defaultReference=$ACS_DAL_REF \
    -DOAPort=$ACS_MANAGER_PORT \
    -Dmanager.debug.rememberOldHandles \
    $ACS_FEDERATION com.cosylab.acs.maci.manager.app.Manager $1 $2 $3 $4 $5 $6 $7 $8 $9
