📄 defaultservercontextfactory.java
字号:
package com.esri.solutions.jitk.services.common;
import java.io.IOException;
import java.util.Properties;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import com.esri.arcgis.interop.AutomationException;
import com.esri.arcgis.server.IServerConnection;
import com.esri.arcgis.server.IServerContext;
import com.esri.arcgis.server.IServerObjectManager;
import com.esri.arcgis.server.ServerConnection;
import com.esri.arcgis.system.ServerInitializer;
import com.esri.arcgis.util.security.Cryptographer;
/**
* Implementation of {@link IServerContextFactory} that will create {@link IServerContext}
* objects based on parameters passed in via a {@link Properties} object.
*
* <p>
* The properties that are required to be passed into this factory are:
*
* <ul>
* <li>HOST: Required. Host name of the ArcGIS Server
* <li>DOMAIN: Required. Domain that contains the specified User (* if local host)
* <li>USERNAME: Required. Username to log into ArcGIS Server (must be a member of agsadmin or agsusers group on host machine)
* <li>PASSWORD: Required. Password to log into ArcGIS Server (can be encrypted or plain-text, depends on value of PASSWORD_ENCRYPTED)
* <li>PASSWORD_ENCRYPTED: Required. Boolean value indicating if the PASSWORD value is encrypted.
* <li>SERVICE: Optional. Name of the ArcGIS Server Service
* <li>SERVICE_TYPE: Optional. Type of ArcGIS Server Service (MapServer, GeocodeServer, etc.). Required if
* SERVICE property is specified.
* </ul>
* </p>
*
* <p>
* If the password is encrypted (PASSWORD_ENCRYPTED = true), then the password will be decrypted before
* attempting to create the connection.
* </p>
*/
public class DefaultServerContextFactory implements IServerContextFactory {
/**
* Contains the connection information for creating a {@link IServerContext}.
*/
private final Properties m_props;
/**
* Logger to use to log messages from this class.
*/
private static final Logger LOG = LogManager.getLogger(DefaultServerContextFactory.class);
/**
* Constructs a new <code>DefaultServerContextFactory</code> object.
*/
public DefaultServerContextFactory () {
m_props = new Properties();
}
/*
* (non-Javadoc)
* @see com.esri.solutions.jitk.services.common.IServerContextFactory#createServerContext()
*/
public IServerContext createServerContext() throws ServicesException {
String host = null;
String domain = null;
String username = null;
String password = null;
String service = null;
String serviceType = null;
boolean passwordEncrypted = false;
Cryptographer crypto = new Cryptographer();
ServerInitializer serverInitializer = null;
IServerConnection serverConnection = null;
IServerObjectManager som = null;
IServerContext context = null;
try {
host = m_props.getProperty("HOST");
domain = m_props.getProperty("DOMAIN");
username = m_props.getProperty("USERNAME");
password = m_props.getProperty("PASSWORD");
if (Boolean.valueOf(m_props.getProperty("PASSWORD_ENCRYPTED")) == null) {
passwordEncrypted = false;
} else {
passwordEncrypted = Boolean.valueOf(m_props.getProperty("PASSWORD_ENCRYPTED"));
}
service = m_props.getProperty("SERVICE");
serviceType = m_props.getProperty("SERVICE_TYPE");
serverInitializer = new ServerInitializer();
if (domain == null) {
throw new ServicesException ("DOMAIN not specified, cannot initialize Server.");
}
if (username == null) {
throw new ServicesException ("USERNAME not specified, cannot initialize Server.");
}
if (passwordEncrypted) {
password = crypto.decrypt(password);
}
serverInitializer.initializeServer(domain, username, password);
if (host == null) {
throw new ServicesException ("HOST not specified, cannot initialize Server");
}
serverConnection = new ServerConnection();
serverConnection.connect(host);
som = serverConnection.getServerObjectManager();
if (service == null) {
service = "";
}
if (serviceType == null) {
serviceType = "";
}
service = service.trim();
serviceType = serviceType.trim();
context = som.createServerContext(service, serviceType);
return context;
} catch (AutomationException e) {
if (context != null) {
try {
context.releaseContext();
} catch (AutomationException e1) {
LOG.warn("Unable to release ServerContext", e1);
} catch (IOException e1) {
LOG.warn("Unable to release ServerContext", e1);
}
}
throw new ServicesException (e);
} catch (IOException e) {
if (context != null) {
try {
context.releaseContext();
} catch (AutomationException e1) {
LOG.warn("Unable to release ServerContext", e1);
} catch (IOException e1) {
LOG.warn("Unable to release ServerContext", e1);
}
}
throw new ServicesException (e);
}
}
/**
* Sets the Connection Info Properties. See the class documentation for the properties
* that are required to be within the {@link Properties} object.
*
* @param props New Connection Information Properties.
*/
public void setConnectionInfo (Properties props) {
if (props != null) {
LOG.info("Connection Info: " + props.toString());
m_props.putAll(props);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -