/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License, version 2 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/gpl-2.0.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program 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 General Public License for more details.
*
*
* Copyright 2005 - 2008 Pentaho Corporation. All rights reserved.
*
* @created Jun 22, 2005
* @author James Dixon
*
*/
package org.pentaho.platform.engine.core.system;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.pentaho.platform.api.engine.IApplicationContext;
import org.pentaho.platform.api.engine.IPentahoSession;
import org.pentaho.platform.api.engine.IPentahoSystemEntryPoint;
import org.pentaho.platform.api.engine.IPentahoSystemExitPoint;
import org.pentaho.platform.api.util.ITempFileDeleter;
public class StandaloneApplicationContext implements IApplicationContext {
private String solutionRootPath;
private String baseUrl;
private String applicationPath;
private Object context;
private Properties properties = new Properties();
private final List<IPentahoSystemEntryPoint> entryPointsList = new ArrayList<IPentahoSystemEntryPoint>();
private final List<IPentahoSystemExitPoint> exitPointsList = new ArrayList<IPentahoSystemExitPoint>();
public StandaloneApplicationContext(final String solutionRootPath, final String applicationPath, final Object context) {
this(solutionRootPath, applicationPath);
this.context = context;
}
public StandaloneApplicationContext(final String solutionRootPath, final String applicationPath) {
//TODO sbarkdull, do we need to consider path separators for windows?
//assert !solutionRootPath.endsWith("\\") : "solutionRootPathin StandaloneApplicationContext appears to be using Windows path separators.";
//assert !applicationPath.endsWith("\\") : "applicationPath in StandaloneApplicationContext appears to be using Windows path separators.";
this.solutionRootPath = solutionRootPath;
this.applicationPath = applicationPath;
baseUrl = null;
}
public void setBaseUrl(final String baseUrl) {
this.baseUrl = baseUrl;
}
public String getFileOutputPath(final String path) {
return solutionRootPath + File.separator + path;
}
public String getSolutionPath(final String path) {
return solutionRootPath + File.separator + path;
}
public void setSolutionRootPath(final String solutionRootPath) {
this.solutionRootPath = solutionRootPath;
}
public File createTempFile(final IPentahoSession session, final String prefix, final String extn, boolean trackFile) throws IOException {
return createTempFile(session, prefix, extn, new File(getSolutionPath("system/tmp")), trackFile); //$NON-NLS-1$
}
public File createTempFile(final IPentahoSession session, final String prefix, final String extn, final File parentDir, boolean trackFile) throws IOException {
ITempFileDeleter fileDeleter = null;
if ((session != null) && trackFile) {
fileDeleter = (ITempFileDeleter)session.getAttribute(ITempFileDeleter.DELETER_SESSION_VARIABLE);
}
final String newPrefix = new StringBuilder().append(prefix).append(session.getId().substring(0, 10)).append('-').toString();
final File file = File.createTempFile(newPrefix, extn, parentDir);
if (fileDeleter != null) {
fileDeleter.trackTempFile(file);
} else {
// There is no deleter, so cleanup on VM exit. (old behavior)
file.deleteOnExit();
}
return file;
}
/*
* (non-Javadoc)
*
* @see org.pentaho.newcode.IApplicationContext#getServerName()
*/
public String getPentahoServerName() {
return ""; //$NON-NLS-1$
}
/*
* (non-Javadoc)
*
* @see org.pentaho.newcode.IApplicationContext#getServerPort()
*/
public int getServerPort() {
// TODO Auto-generated method stub
return 0;
}
public String getBaseUrl() {
return baseUrl;
}
public String getApplicationPath(final String path) {
return applicationPath + File.separator + path;
}
public String getProperty(final String key) {
return properties.getProperty(key);
}
public String getProperty(final String key, final String defaultValue) {
return properties.getProperty(key, defaultValue);
}
public void setProperties(final Properties props) {
properties = props;
}
public void addEntryPointHandler(final IPentahoSystemEntryPoint entryPoint) {
entryPointsList.add(entryPoint);
}
public void removeEntryPointHandler(final IPentahoSystemEntryPoint entryPoint) {
entryPointsList.remove(entryPoint);
}
public void addExitPointHandler(final IPentahoSystemExitPoint exitPoint) {
exitPointsList.add(exitPoint);
}
public void removeExitPointHandler(final IPentahoSystemExitPoint exitPoint) {
exitPointsList.remove(exitPoint);
}
public void invokeEntryPoints() {
for (int i = 0; i < entryPointsList.size(); i++) {
((IPentahoSystemEntryPoint) entryPointsList.get(i)).systemEntryPoint();
}
}
public void invokeExitPoints() {
for (int i = 0; i < exitPointsList.size(); i++) {
((IPentahoSystemExitPoint) entryPointsList.get(i)).systemExitPoint();
}
}
public Object getContext() {
return context;
}
public void setContext(final Object context) {
this.context = context;
}
public String getSolutionRootPath() {
return solutionRootPath;
}
}