/*=============================================================================*
* Copyright 2006 The Apache Software Foundation
*
* 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 org.apache.muse.tools.generator.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import org.apache.muse.core.AbstractEnvironment;
import org.apache.muse.util.FileUtils;
import org.apache.muse.util.messages.Messages;
import org.apache.muse.util.messages.MessagesFactory;
import org.apache.muse.ws.addressing.EndpointReference;
/**
*
* An Environment that is local to a JVM. It uses the java.io.File class
* to access the file system and resolve any resource paths.
*
* @author Dan Jemiolo (danj)
* @author Andrew Eberbach (aeberbac)
*
*/
public class LocalEnvironment extends AbstractEnvironment
{
//
// Used to lookup all exception messages
//
private static Messages _MESSAGES = MessagesFactory.get(LocalEnvironment.class);
private static final URI LOCAL_URI = URI.create("http://localhost");
private EndpointReference _deploy = new EndpointReference(LOCAL_URI);
private EndpointReference _target = null;
private File _realDirectory = null;
public LocalEnvironment()
{
this(true);
}
public LocalEnvironment(File realDirectory)
{
this(realDirectory, true);
}
public LocalEnvironment(boolean createSoapClient)
{
this(FileUtils.CURRENT_DIR, createSoapClient);
}
public LocalEnvironment(File realDirectory, boolean createSoapClient)
{
super(createSoapClient);
setRealDirectory(realDirectory);
}
public EndpointReference getDeploymentEPR()
{
return new EndpointReference(_deploy);
}
public File getRealDirectory()
{
return _realDirectory;
}
public EndpointReference getTargetEPR()
{
return _target;
}
public final void setRealDirectory(File realDirectory)
{
if (realDirectory == null)
throw new NullPointerException(_MESSAGES.get("NullRealDirectory"));
_realDirectory = realDirectory;
}
public void setTargetEPR(EndpointReference target)
{
_target = target;
}
public URL getDataResource(String path)
{
if (path == null)
throw new NullPointerException(_MESSAGES.get("NullResourcePath"));
File file = new File(_realDirectory, path);
try
{
return file.toURL();
}
catch (MalformedURLException error)
{
throw new RuntimeException(error);
}
}
public InputStream getDataResourceStream(String path)
{
if (path == null)
throw new NullPointerException(_MESSAGES.get("NullResourcePath"));
File file = new File(getRealDirectory(), path);
try
{
return new FileInputStream(file);
}
catch (FileNotFoundException error)
{
Object[] filler = { file.getAbsolutePath() };
String message = _MESSAGES.get("InvalidFile", filler);
throw new RuntimeException(message);
}
}
}