Skip to content

Commit

Permalink
Fixes issue #3478 - Add PiranhaConfiguration class (#3479)
Browse files Browse the repository at this point in the history
  • Loading branch information
mnriem authored Jul 11, 2023
1 parent 80db707 commit 18666ca
Show file tree
Hide file tree
Showing 6 changed files with 348 additions and 343 deletions.
17 changes: 17 additions & 0 deletions core/api/src/main/java/cloud/piranha/core/api/Piranha.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@
*/
public interface Piranha {

/**
* Get the configuration.
*
* @return the configuration.
*/
default PiranhaConfiguration getConfiguration() {
return null;
}

/**
* Service the request and response.
*
Expand All @@ -47,4 +56,12 @@ public interface Piranha {
*/
void service(WebApplicationRequest request, WebApplicationResponse response)
throws IOException, ServletException;

/**
* Set the configuration.
*
* @param configuration the configuration.
*/
default void setConfiguration(PiranhaConfiguration configuration) {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cloud.piranha.core.api;

import java.io.File;

/**
* Piranha configuration.
*
* @author Manfred Riem ([email protected])
*/
public interface PiranhaConfiguration {

/**
* Get the value as a boolean.
*
* @param key the key.
* @param defaultValue the default value if the key is not found.
* @return the value.
*/
boolean getBoolean(String key, boolean defaultValue);

/**
* Get the value as a class.
*
* @param key the key.
* @return the value.
*/
Class<?> getClass(String key);

/**
* Get the value as a File.
*
* @param key the key.
* @return the value.
*/
File getFile(String key);

/**
* Get the value as an int.
*
* @param key the key.
* @return the value.
*/
Integer getInteger(String key);

/**
* Get the value as a long.
*
* @param key the key.
* @return the value.
*/
Long getLong(String key);

/**
* Get the value as a string.
*
* @param key the key.
* @return the value (or null if not set).
*/
String getString(String key);

/**
* Set the boolean value.
*
* @param key the key.
* @param value the boolean value.
*/
void setBoolean(String key, Boolean value);

/**
* Set the class value.
*
* @param key the key.
* @param value the class value.
*/
void setClass(String key, Class<?> value);

/**
* Set the File value.
*
* @param key the key.
* @param value the value.
*/
void setFile(String key, File value);

/**
* Set the integer value.
*
* @param key the key.
* @param value the value.
*/
void setInteger(String key, Integer value);

/**
* Set the long value.
*
* @param key the key.
* @param value the value.
*/
void setLong(String key, Long value);

/**
* Set the string value.
*
* @param key the key.
* @param value the value.
*/
void setString(String key, String value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* Copyright (c) 2002-2023 Manorrock.com. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package cloud.piranha.core.impl;

import cloud.piranha.core.api.PiranhaConfiguration;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
* The default Piranha configuration implementation.
*
* @author Manfred Riem ([email protected])
*/
public class DefaultPiranhaConfiguration implements PiranhaConfiguration {

/**
* Stores the configuration.
*/
private final Map<String, Object> configuration = new HashMap<>();

@Override
public boolean getBoolean(String key, boolean defaultValue) {
if (configuration.containsKey(key)) {
return (boolean) configuration.get(key);
}
return defaultValue;
}

@Override
public Class<?> getClass(String key) {
return (Class<?>) configuration.get(key);
}

@Override
public File getFile(String key) {
return (File) configuration.get(key);
}

@Override
public Integer getInteger(String key) {
return (Integer) configuration.get(key);
}

@Override
public Long getLong(String key) {
return (Long) configuration.get(key);
}

@Override
public String getString(String key) {
return (String) configuration.get(key);
}

@Override
public void setBoolean(String key, Boolean value) {
configuration.put(key, value);
}

@Override
public void setClass(String key, Class<?> value) {
configuration.put(key, value);
}

@Override
public void setFile(String key, File value) {
configuration.put(key, value);
}

@Override
public void setInteger(String key, Integer value) {
configuration.put(key, value);
}

@Override
public void setLong(String key, Long value) {
configuration.put(key, value);
}

@Override
public void setString(String key, String value) {
configuration.put(key, value);
}
}
Loading

0 comments on commit 18666ca

Please sign in to comment.