![JAR search and dependency download from the Maven repository](/logo.png)
com.ovea.tajin.server.ContainerConfiguration Maven / Gradle / Ivy
/**
* Copyright (C) 2011 Ovea
*
* 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 com.ovea.tajin.server;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
public final class ContainerConfiguration {
public static final int DEFAULT_PORT = 8080;
public static final String DEFAULT_CONTEXT_PATH = "/";
public static final String DEFAULT_WEBAPP_ROOT = "src/main/webapp";
final java.util.Properties properties = new java.util.Properties();
private ContainerConfiguration() {
port(DEFAULT_PORT);
context(DEFAULT_CONTEXT_PATH);
webappRoot(DEFAULT_WEBAPP_ROOT);
}
/* Fluent config */
public Container buildContainer(Server type) {
return buildContainer(type.serverClass());
}
public Container buildContainer(String containerClass) {
notNull(containerClass, "Container type " + Arrays.deepToString(Server.values()) + " or container class");
Class clazz = null;
for (Server container : Server.values()) {
if (container.name().equalsIgnoreCase(containerClass)) {
clazz = loadServerClass(container.serverClass());
break;
}
}
if (clazz == null) clazz = loadServerClass(containerClass);
try {
return clazz.getConstructor(ContainerConfiguration.class).newInstance(ContainerConfiguration.from(this));
} catch (InvocationTargetException e) {
if (e.getTargetException() instanceof RuntimeException) throw (RuntimeException) e.getTargetException();
else throw new RuntimeException(e.getTargetException().getMessage(), e.getTargetException());
} catch (Exception e) {
throw new IllegalArgumentException("Cannot instanciate class [" + clazz + "]. Ensure there is a public constructor having a parameter of type " + ContainerConfiguration.class.getName(), e);
}
}
/* fluent config */
public ContainerConfiguration serverClassPath(String serverClassPath) {
notNull(serverClassPath, "Server classpath");
return serverClassPath(toUrl(serverClassPath));
}
public ContainerConfiguration serverClassPath(File... paths) {
notNull(paths, "Server classpath");
List urls = new ArrayList<>(paths.length);
for (File path : paths) urls.add(pathAsURL(path.getAbsolutePath()));
return serverClassPath(urls.toArray(new URL[urls.size()]));
}
public ContainerConfiguration serverClassPath(URL... locations) {
notNull(locations, "Server classpath");
properties.put(Properties.SERVER_CLASSPATH, locations);
return this;
}
public boolean hasServerClassPath() {
return properties.get(Properties.SERVER_CLASSPATH) != null;
}
public URL[] serverClassPath() {
URL[] locations = (URL[]) properties.get(Properties.SERVER_CLASSPATH);
return locations == null ? new URL[0] : locations;
}
public ContainerConfiguration webappClassPath(String webappClassPath) {
notNull(webappClassPath, "Webapp classpath");
return webappClassPath(toUrl(webappClassPath));
}
public ContainerConfiguration webappClassPath(File... paths) {
notNull(paths, "Webapp classpath");
List urls = new ArrayList<>(paths.length);
for (File path : paths) urls.add(pathAsURL(path.getAbsolutePath()));
return webappClassPath(urls.toArray(new URL[urls.size()]));
}
public ContainerConfiguration webappClassPath(URL... locations) {
notNull(locations, "Webapp classpath");
properties.put(Properties.WEBAPP_CLASSPATH, locations);
return this;
}
public boolean hasWebappClassPath() {
return properties.get(Properties.WEBAPP_CLASSPATH) != null;
}
public URL[] webappClassPath() {
URL[] locations = (URL[]) properties.get(Properties.WEBAPP_CLASSPATH);
return locations == null ? new URL[0] : locations;
}
public ContainerConfiguration webappRoot(String webappRoot) {
notNull(webappRoot, "Webapp root");
return webappRoot(new File(webappRoot));
}
public ContainerConfiguration webappRoot(File webappRoot) {
notNull(webappRoot, "Webapp root");
properties.put(Properties.WEBAPP_ROOT, webappRoot);
return this;
}
public File webappRoot() {
return (File) properties.get(Properties.WEBAPP_ROOT);
}
public ContainerConfiguration context(String contextPath) {
notNull(contextPath, "Webapp context path");
properties.put(Properties.CONTEXT, contextPath.startsWith("/") ? contextPath : "/" + contextPath);
return this;
}
public String context() {
return (String) properties.get(Properties.CONTEXT);
}
public ContainerConfiguration port(int port) {
if (port < 0 || port > 65535) throw new IllegalArgumentException("Invalid port: " + port);
properties.put(Properties.PORT, port);
return this;
}
public int port() {
return (Integer) properties.get(Properties.PORT);
}
public ContainerConfiguration overlays(String... paths) {
properties.put(Properties.OVERLAYS, paths);
return this;
}
public ContainerConfiguration overlays(String paths) {
return overlays(paths.split(",|;|:"));
}
public String[] overlays() {
String[] locations = (String[]) properties.get(Properties.OVERLAYS);
return locations == null ? new String[0] : locations;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder("ContainerConfiguration:\n");
for (Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy