/*******************************************************************************
* Copyright (c) 2012 VMWare, Inc.
* 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:
* VMWare, Inc. - initial API and implementation
*******************************************************************************/
package org.grails.ide.eclipse.runonserver;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.Assert;
import org.eclipse.core.runtime.CoreException;
import org.grails.ide.eclipse.core.GrailsCoreActivator;
import org.grails.ide.eclipse.core.internal.GrailsNature;
import org.springsource.ide.eclipse.commons.core.SpringCorePreferences;
import org.grails.ide.eclipse.runonserver.ui.RunOnServerPropertyPage;
/**
* Core support for setting / getting project properties related to run on server functionality.
* <p>
* See {@link RunOnServerPropertyPage} for UI counterpart.
*
* @since 2.6
* @author Kris De Volder
*/
public class RunOnServerProperties {
public static interface RunOnServerPropertiesListener {
public void envChanged(IProject project, String old, String env);
public void incrementalChanged(IProject project, boolean old, boolean isIncremental);
}
/**
* Property specifying 'env' parameter, used to deploy an app with 'run on server'. This is private, should only get/set this
* by calling helper methods in this class.
*/
private static final String ENV_PROPERTY = RunOnServerProperties.class.getName()+ ".ENV";
/**
* Property specifying whether we use an incremental strategy for creating the deplyment artefacts.
* When incremental is set, we try to substitute the .class files generated by the Greclipse
* compiler into the Grails generated war thereby avoiding war builds on each little change.
* <p>
* When incremental is off, each (re)deploy will require a full war build and the contents of
* the war will be deployed as is.
*/
private static final String INCREMENTAL_PROPERTY = RunOnServerProperties.class.getName()+ ".INCREMENTAL";
public static final String DEFAULT_ENV = "prod";
public static final boolean DEFAULT_INCREMENTAL = true;
private static RunOnServerPropertiesListener listener;
/**
* Set property specifying 'env' parameter, used to deploy an app with 'run on server'.
* @throws CoreException
*/
public static synchronized void setEnv(IProject project, String env) throws CoreException {
Assert.isLegal(GrailsNature.isGrailsAppProject(project));
String old = getEnv(project);
if (!old.equals(env)) {
SpringCorePreferences prefs = getPreferences(project);
envChanged(project, old, env);
prefs.putString(ENV_PROPERTY, env);
}
}
public static synchronized void setIncremental(IProject project, boolean isIncremental) throws CoreException {
Assert.isLegal(GrailsNature.isGrailsAppProject(project));
boolean old = getIncremental(project);
if (old!=isIncremental) {
SpringCorePreferences prefs = getPreferences(project);
incrementalChanged(project, old, isIncremental);
prefs.putBoolean(INCREMENTAL_PROPERTY, isIncremental);
}
}
public static boolean getIncremental(IProject project) {
return getPreferences(project).getBoolean(INCREMENTAL_PROPERTY, RunOnServerProperties.DEFAULT_INCREMENTAL);
}
private static SpringCorePreferences getPreferences(IProject project) {
return SpringCorePreferences.getProjectPreferences(project, GrailsCoreActivator.PLUGIN_ID);
}
private static void envChanged(IProject project, String old, String env) {
listener.envChanged(project, old, env);
}
private static void incrementalChanged(IProject project, boolean old, boolean isIncremental) {
listener.incrementalChanged(project, old, isIncremental);
}
/**
* Get property specifying 'env' parameter, used to deploy an app with 'run on server'. This never
* returns null. If the property was not previously set, it returns the DEFAULT_ENV value.
*/
public static String getEnv(IProject project) {
SpringCorePreferences prefs = getPreferences(project);
return prefs.getString(ENV_PROPERTY, RunOnServerProperties.DEFAULT_ENV);
}
public static void addEnvChangeListener(RunOnServerPropertiesListener l) {
Assert.isLegal(listener==null, "Only supporting and expecting one listener");
listener = l;
}
}