/*******************************************************************************
* Copyright (c) 2009, 2010 Innovation Gate GmbH.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Innovation Gate GmbH - initial API and implementation
******************************************************************************/
package de.innovationgate.eclipse.wgadesigner.models;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import net.java.dev.genesis.annotation.Action;
import net.java.dev.genesis.annotation.DataProvider;
import net.java.dev.genesis.annotation.NotBound;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.launching.JavaRuntime;
import org.eclipse.jdt.launching.environments.IExecutionEnvironment;
import de.innovationgate.eclipse.wgadesigner.WGADesignerPlugin;
import de.innovationgate.eclipse.wgadesigner.editors.helpers.WGADeployment;
import de.innovationgate.eclipse.wgadesigner.natures.IncompatibleWGAConfigVersion;
import de.innovationgate.eclipse.wgadesigner.natures.WGARuntime;
import de.innovationgate.eclipse.wgadesigner.natures.WGARuntimeConfiguration;
import de.innovationgate.utils.WGUtils;
import de.innovationgate.wga.model.AbstractModel;
import de.innovationgate.wga.model.Model;
import de.innovationgate.wga.model.ValidationError;
public class WGARuntimeConfigurationModel extends AbstractModel {
private WGARuntimeConfiguration _wgaRuntimeConfiguration;
private WGARuntime _wgaRuntime;
// the current wga configuration of the runtime - might be null
//private WGAConfiguration _wgaConfig;
public void init(WGARuntime runtime) throws IncompatibleWGAConfigVersion {
_wgaRuntime = runtime;
_wgaRuntimeConfiguration = runtime.getConfiguration();
/*
try {
_wgaConfig = runtime.retrieveWGAConfig(false);
} catch (IOException e) {
// should not happen bc. we do not try to generate a default config
WGADesignerPlugin.getDefault().logError(e.getMessage(), e);
}*/
}
@Override
public void saveChanges() throws IOException {
try {
_wgaRuntime.flushConfig();
/*
if (_wgaConfig != null) {
_wgaRuntime.saveWGAConfig(_wgaConfig);
}*/
} catch (Exception e) {
IOException io = new IOException("Unable to save wga runtime config: " + e.getMessage());
io.setStackTrace(e.getStackTrace());
throw io;
}
}
@DataProvider(objectField="wgaDeployment")
@Action
public List<WGADeployment> populateWGADeployments() {
return WGADesignerPlugin.getDefault().getWGADeploymentManager().getDeployments();
}
public WGADeployment getWgaDeployment() {
String distValue = _wgaRuntimeConfiguration.getWgaDistribution();
Iterator<WGADeployment> deployments = populateWGADeployments().iterator();
while (deployments.hasNext()) {
WGADeployment deployment = deployments.next();
if (deployment.getName().equals(distValue)) {
return deployment;
}
}
return null;
}
public void setWgaDeployment(WGADeployment deployment) {
if (deployment != null) {
_wgaRuntimeConfiguration.setWgaDistribution(deployment.getName());
fireModelChanged();
}
}
@NotBound
public String[] getTomcatLibraryNames() {
File dir = _wgaRuntime.getCatalinaLibDir().getLocation().toFile();
String[] names;
names = dir.list(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.endsWith(".jar");
}
});
return names;
}
public void addTomcatLibrary(File file) throws IOException {
File dir = _wgaRuntime.getCatalinaLibDir().getLocation().toFile();
File target = new File(dir, file.getName());
FileInputStream in = new FileInputStream(file);
FileOutputStream out = new FileOutputStream(target);
try {
WGUtils.inToOut(in, out, 2048);
} finally {
in.close();
out.close();
}
try {
_wgaRuntime.getCatalinaLibDir().refreshLocal(IProject.DEPTH_ONE, null);
} catch (CoreException e) {
}
}
public boolean hasTomcatLibrary(String name) {
return Arrays.asList(getTomcatLibraryNames()).contains(name);
}
public boolean deleteLibrary(String filename) {
File dir = _wgaRuntime.getCatalinaLibDir().getLocation().toFile();
boolean result = new File(dir, filename).delete();
try {
_wgaRuntime.getCatalinaLibDir().refreshLocal(IProject.DEPTH_ONE, null);
} catch (CoreException e) {
}
return result;
}
@NotBound
public WGARuntime getWgaRuntime() {
return _wgaRuntime;
}
@Override
public void reload() throws IOException {
try {
_wgaRuntimeConfiguration = _wgaRuntime.reloadConfig();
/*
try {
_wgaConfig = _wgaRuntime.retrieveWGAConfig(false);
} catch (Exception e) {
WGADesignerPlugin.getDefault().logError("Unable to reload wga configuration.", e);
}*/
} catch (Exception e) {
IOException ioe = new IOException("Unable to reload wga runtime configuration model: '" + e.getMessage() + "'.");
ioe.setStackTrace(e.getStackTrace());
throw ioe;
}
}
@Override
public List<ValidationError> validate() {
return null;
}
/*
public boolean isOutputTMLWarningsOnConsole() {
if (_wgaConfig != null) {
return _wgaConfig.isWarningsOutputOnConsole();
} else {
return false;
}
}
public void setOutputTMLWarningsOnConsole(boolean enabled) {
if (_wgaConfig != null) {
_wgaConfig.setWarningsOutputOnConsole(enabled);
fireModelChanged();
}
}*/
public String getJvmOptions() {
return _wgaRuntimeConfiguration.getJvmOptions();
}
public void setJvmOptions(String jvmOptions) {
_wgaRuntimeConfiguration.setJvmOptions(jvmOptions);
fireModelChanged();
}
@DataProvider(objectField="executionEnvironment")
@Action
public List<WGAExecutionEnvironment> populateExecutionEnvironment() {
List<WGAExecutionEnvironment> jres = new ArrayList<WGAExecutionEnvironment>();
jres.add(WGAExecutionEnvironment.DEFAULT_INSTANCE);
IExecutionEnvironment[] environments = JavaRuntime.getExecutionEnvironmentsManager().getExecutionEnvironments();
for (IExecutionEnvironment environment : environments) {
jres.add(new WGAExecutionEnvironment(environment));
}
Collections.sort(jres, new Comparator<WGAExecutionEnvironment>() {
public int compare(WGAExecutionEnvironment o1, WGAExecutionEnvironment o2) {
if (o1.getKey().equals(WGAExecutionEnvironment.DEFAULT_INSTANCE_ID)) {
return -1;
} else if (o2.getKey().equals(WGAExecutionEnvironment.DEFAULT_INSTANCE_ID)) {
return 1;
} else {
return o1.toString().compareTo(o2.toString());
}
}
});
return jres;
}
public WGAExecutionEnvironment getExecutionEnvironment() {
if (_wgaRuntimeConfiguration.getExecutionEnvironmentId() == null) {
return WGAExecutionEnvironment.DEFAULT_INSTANCE;
} else {
return WGAExecutionEnvironment.findById(_wgaRuntimeConfiguration.getExecutionEnvironmentId());
}
}
public void setExecutionEnvironment(WGAExecutionEnvironment environment) {
if (environment != null && !environment.getKey().equals(WGAExecutionEnvironment.DEFAULT_INSTANCE_ID)) {
_wgaRuntimeConfiguration.setExecutionEnvironmentId(environment.getKey());
} else {
_wgaRuntimeConfiguration.setExecutionEnvironmentId(null);
}
fireModelChanged();
}
public URL getRemoteLocation() {
return _wgaRuntimeConfiguration.getRemoteLocation();
}
public void setRemoteLocation(URL remoteLocation) {
_wgaRuntimeConfiguration.setRemoteLocation(remoteLocation);
fireModelChanged();
}
@Override
public boolean isEditable(String propertyName) {
return true;
}
}