org.coos.messaging.BaseCOContainer Maven / Gradle / Ivy
/**
* COOS - Connected Objects Operating System (www.connectedobjects.org).
*
* Copyright (C) 2009 Telenor ASA and Tellu AS. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*
* You may also contact one of the following for additional information:
* Telenor ASA, Snaroyveien 30, N-1331 Fornebu, Norway (www.telenor.no)
* Tellu AS, Hagalokkveien 13, N-1383 Asker, Norway (www.tellu.no)
*/
package org.coos.messaging;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.coos.messaging.COContainer;
import org.coos.util.macro.MacroSubstituteReader;
/**
* Base class with default implementation of the COContainer interface.
*
* @author Robert Bjærum, Tellu AS
*
*/
public class BaseCOContainer implements COContainer {
public static final String COOS_CONFIG_PATH = "/org/coos/config";
public final static String DEFAULT_COOS_CONFIG_DIR = "./coosConfig";
protected String configDir = DEFAULT_COOS_CONFIG_DIR;
public String getConfigDir() {
return configDir;
}
public void setConfigDir(String configDir) {
this.configDir = configDir;
}
public Object getObject(String name) {
return null;
}
/**
* Return resource as Stream.
*
* The resource name will be attempted resolved as follows. A baseName
is formed following the same
* rule as for Class.getResourceAsStream(), removing any "/" prefix.
*
* 1. First, try File("./", baseName)
*
* 2. Then, try File(configDir, baseName
)
*
* 3. Then, try getResourceAsStream(resourceName)
*
* 4. Then, try getResourceAsStream(COOS_CONFIG_PATH
and try the resulting path name.
*
* 5. If failed, throw IOException
*
* @param resourceName complete path to resource or base name of resource
* @throws IOException if resource get fails
* @see Class
*/
public InputStream getResource(String resourceName) throws IOException {
InputStream is = null;
String baseName;
if (resourceName.isEmpty()) {
return null;
}
if (resourceName.startsWith("/")) {
baseName = resourceName.substring(1);
} else {
baseName = resourceName;
resourceName = "/" + resourceName;
}
try {
File file = new File(".", baseName);
is = file.toURI().toURL().openStream();
is = substitute(is);
if (is != null) {
return is;
}
} catch (Exception ignore) {
}
try {
File file = new File(configDir, baseName);
is = file.toURI().toURL().openStream();
is = substitute(is);
if (is != null) {
return is;
}
} catch (Exception ignore) {
}
try {
is = this.getClass().getResourceAsStream(resourceName);
is = substitute(is);
if (is != null) {
return is;
}
} catch (Exception ignore) {
}
is = this.getClass().getResourceAsStream(COOS_CONFIG_PATH + resourceName);
is = substitute(is);
return is;
}
@SuppressWarnings("unchecked")
public Class loadClass(String className) throws ClassNotFoundException {
return Class.forName(className);
}
protected InputStream substitute(InputStream is) throws IOException {
if (is == null) {
throw new IOException("Inputstream cannot be null");
}
InputStreamReader isr = new InputStreamReader(is);
MacroSubstituteReader msr = new MacroSubstituteReader(isr);
String substituted = msr.substituteMacros();
is = new ByteArrayInputStream(substituted.getBytes());
return is;
}
@Override public void start() throws Exception {
// TODO Auto-generated method stub
}
@Override public void stop() throws Exception {
// TODO Auto-generated method stub
}
}