/*******************************************************************************
* Copyright 2009, 2010 Innovation Gate GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package de.innovationgate.wga.common.beans.csconfig.v1;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.vfs.FileObject;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.alias.CannotResolveClassException;
import com.thoughtworks.xstream.io.xml.DomDriver;
import de.innovationgate.utils.XStreamUtils;
import de.innovationgate.wga.common.Constants;
import de.innovationgate.wga.common.DesignDirectory;
public class CSConfig {
protected static final XStream XSTREAM = new XStream(new DomDriver());
public static Class getCSConfigClassForCompliance(String compliance) {
if (compliance.equals(VERSIONCOMPLIANCE_WGA50) || compliance.equals(VERSIONCOMPLIANCE_WGA51) ||
compliance.equals(VERSIONCOMPLIANCE_WGA52) || compliance.equals(VERSIONCOMPLIANCE_WGA53) || compliance.equals(VERSIONCOMPLIANCE_WGA54)) {
return de.innovationgate.wga.common.beans.csconfig.v3.CSConfig.class;
}
else if (compliance.equals(VERSIONCOMPLIANCE_WGA41)) {
return de.innovationgate.wga.common.beans.csconfig.v2.CSConfig.class;
}
else {
return de.innovationgate.wga.common.beans.csconfig.v1.CSConfig.class;
}
}
public static CSConfig instantiateCSConfigForCompliance(String compliance) {
Class clazz = getCSConfigClassForCompliance(compliance);
try {
CSConfig csconfig = (CSConfig) clazz.newInstance();
return csconfig;
}
catch (InstantiationException e) {
throw new RuntimeException("Cannot instantiate " + clazz.getName(), e);
}
catch (IllegalAccessException e) {
throw new RuntimeException("Cannot instantiate " + clazz.getName(), e);
}
}
public static final String VERSIONCOMPLIANCE_WGA3 = "wga3";
public static final String VERSIONCOMPLIANCE_WGA4 = "wga4";
public static final String VERSIONCOMPLIANCE_WGA41 = "wga4.1";
public static final String VERSIONCOMPLIANCE_WGA50 = "wga5.0";
public static final String VERSIONCOMPLIANCE_WGA51 = "wga5.1";
public static final String VERSIONCOMPLIANCE_WGA52 = "wga5.2";
public static final String VERSIONCOMPLIANCE_WGA53 = "wga5.3";
public static final String VERSIONCOMPLIANCE_WGA54 = "wga5.4";
private String initScript = null;
private String connectionScript = null;
private int anonymousAccessLevel = -1;
private int defaultAccessLevel = -1;
private String versionCompliance = VERSIONCOMPLIANCE_WGA54;
private List publisherOptions = new ArrayList();
private List encoderMappings = new ArrayList();
private List elementMappings = new ArrayList();
private List remoteActions = new ArrayList();
private List mediaKeys = new ArrayList();
private List roles = new ArrayList();
private List jobDefinitions = new ArrayList();
private PluginConfig pluginConfig = null;
public CSConfig() {
super();
}
public static CSConfig load(File file) throws IOException, InvalidCSConfigVersionException {
try {
CSConfig csConfig = (CSConfig) XStreamUtils.loadUtf8FromFile(XSTREAM, file);
csConfig.init();
return csConfig;
}
catch (CannotResolveClassException e) {
throw new InvalidCSConfigVersionException(file);
}
}
public static CSConfig load(InputStream in) throws IOException, InvalidCSConfigVersionException {
return load(in, false);
}
public static CSConfig load(InputStream in, boolean forceClose) throws IOException, InvalidCSConfigVersionException {
boolean marked = false;
try {
if (in.markSupported()) {
in.mark(1024 * 64);
marked = true;
}
CSConfig csConfig = (CSConfig) XStreamUtils.loadUtf8FromInputStream(XSTREAM, in, forceClose);
csConfig.init();
return csConfig;
}
catch (CannotResolveClassException e) {
if (marked) {
try {
in.reset();
throw new InvalidCSConfigVersionException(in);
}
catch (IOException ee) {
}
}
throw new InvalidCSConfigVersionException();
}
}
public static CSConfig load(FileObject file) throws IOException, InvalidCSConfigVersionException {
try {
CSConfig csConfig = (CSConfig) XStreamUtils.loadUtf8FromFileObject(XSTREAM, file);
csConfig.init();
return csConfig;
}
catch (CannotResolveClassException e) {
throw new InvalidCSConfigVersionException(file);
}
}
public void write(File file) throws IOException {
XStreamUtils.writeUtf8ToFile(this, XSTREAM, file);
}
public void write(FileObject file) throws IOException {
XStreamUtils.writeUtf8ToFileObject(this, XSTREAM, file);
}
public List getElementMappings() {
return elementMappings;
}
public void setElementMappings(List elementMappings) {
this.elementMappings = elementMappings;
}
public List getEncoderMappings() {
return encoderMappings;
}
public void setEncoderMappings(List encoderMappings) {
this.encoderMappings = encoderMappings;
}
public PluginConfig getPluginConfig() {
return pluginConfig;
}
public void setPluginConfig(PluginConfig pluginConfig) {
this.pluginConfig = pluginConfig;
}
public List getPublisherOptions() {
return publisherOptions;
}
public void setPublisherOptions(List publisherOptions) {
this.publisherOptions = publisherOptions;
}
public List getRemoteActions() {
return remoteActions;
}
public void setRemoteActions(List remoteActions) {
this.remoteActions = remoteActions;
}
public void init() {
if (getMediaKeys() == null) {
setMediaKeys(new ArrayList());
}
if (getRoles() == null) {
setRoles(new ArrayList());
}
if (getJobDefinitions() == null) {
setJobDefinitions(new ArrayList());
}
if (getVersionCompliance() == null) {
setVersionCompliance(VERSIONCOMPLIANCE_WGA51);
}
if (getPluginConfig() != null) {
if (getPluginConfig().getDependencies() == null) {
getPluginConfig().setDependencies(new ArrayList());
}
if (getPluginConfig().getTitle() == null) {
getPluginConfig().setTitle("(no title)");
}
if (getPluginConfig().getPersonalisationMode() == 0) {
getPluginConfig().setPersonalisationMode(Constants.PERSMODE_LOGIN);
}
}
}
public List getMediaKeys() {
return mediaKeys;
}
public void setMediaKeys(List mediaKeys) {
this.mediaKeys = mediaKeys;
}
public RemoteAction findRemoteAction(String moduleName) {
Iterator actions = getRemoteActions().iterator();
while (actions.hasNext()) {
RemoteAction action = (RemoteAction) actions.next();
if (action.getModuleName().equalsIgnoreCase(moduleName)) {
return action;
}
}
return null;
}
public PublisherOption findPublisherOption(String name) {
Iterator options = getPublisherOptions().iterator();
while (options.hasNext()) {
PublisherOption option = (PublisherOption) options.next();
if (option.getName().equalsIgnoreCase(name)) {
return option;
}
}
return null;
}
public String getInitScript() {
return initScript;
}
public void setInitScript(String initScript) {
this.initScript = initScript;
}
public int getAnonymousAccessLevel() {
return anonymousAccessLevel;
}
public void setAnonymousAccessLevel(int anonymousAccessLevel) {
this.anonymousAccessLevel = anonymousAccessLevel;
}
public int getDefaultAccessLevel() {
return defaultAccessLevel;
}
public void setDefaultAccessLevel(int defaultAccessLevel) {
this.defaultAccessLevel = defaultAccessLevel;
}
public List getJobDefinitions() {
return jobDefinitions;
}
public void setJobDefinitions(List jobDefinitions) {
this.jobDefinitions = jobDefinitions;
}
public List getRoles() {
return roles;
}
public void setRoles(List roles) {
this.roles = roles;
}
public String getVersionCompliance() {
return versionCompliance;
}
public void setVersionCompliance(String versionCompliance) {
this.versionCompliance = versionCompliance;
}
public String getConnectionScript() {
return connectionScript;
}
public void setConnectionScript(String connectionScript) {
this.connectionScript = connectionScript;
}
public boolean versionComplianceIsAtLeast(String targetCompliance) {
return versionComplianceIsAtLeast(getVersionCompliance(), targetCompliance);
}
public static boolean versionComplianceIsAtLeast(String complianceToTest, String targetCompliance) {
String currentNumericPart = complianceToTest.substring(3);
double currentVersion = Double.parseDouble(currentNumericPart);
String targetNumericPart = targetCompliance.substring(3);
double targetVersion = Double.parseDouble(targetNumericPart);
return currentVersion >= targetVersion;
}
/**
* Tries to determine the minimum WGA version of the given csconfig.xml data.
* Fails silently if something goes wrong.
* @param in The data of the csconfig.xml
* @return A string describing the target version of the csconfig.xml
*/
public static String determineMinimumWGAVersion(InputStream in) {
String version = "(unknown)";
try {
Document doc = (new SAXReader()).read(in);
Element wgaVersionElement = (Element) doc.selectSingleNode("//minimumWGAVersion");
version = wgaVersionElement.elementText("majorVersion") + "." + wgaVersionElement.elementText("minorVersion") + "." + wgaVersionElement.elementText("maintenanceVersion");
}
catch (Exception ee) {
}
return version;
}
public String getDesignDefinitionFileName() {
return DesignDirectory.SYNCINFO_FILE;
}
}