<!-- add context params in your web.xml -->
<web-app ...>
<context-param>
<param-name>ROLE-ADMIN</param-name>
</param-value>admin</param-value>
</context-param>
</web-app>
USAGE:
<!-- method usage -->
public void someMethod(ServletConfig config) {
ServletContext application = config.getServletContext();
String adminRole = application.getInitParameter("ROLE-ADMIN");
}
<!-- jsp scriplet usage-->
if (userRole.equals(application.getInitParameter("ROLE-ADMIN")))
{
//do something
}
<!-- EL usage -->
<c:if test="${userRole eq initParam.ROLE-ADMIN}">
do something
</c:if>
Tag Archive for java
using context init parameters in web.xml
A generic DBCP connection pool Spring bean
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
<property name="maxIdle" value="10"/>
<property name="maxActive" value="100"/>
<property name="maxWait" value="10000"/>
<property name="validationQuery" value="select 1"/>
<property name="testOnBorrow" value="false"/>
<property name="testWhileIdle" value="true"/>
<property name="timeBetweenEvictionRunsMillis" value="1200000"/>
<property name="minEvictableIdleTimeMillis" value="1800000"/>
<property name="numTestsPerEvictionRun" value="5"/>
<property name="defaultAutoCommit" value="false"/>
</bean>
Create XML from Template and Properties
import groovy.text.XmlTemplateEngine
import java.util.Properties
import java.io.File
import java.io.FileWriter
//Takes a java props file, an XML template file and creates the given output file
void createFile(propertiesFile, templateFileName, outputFileName) {
// read properties file given
def props = new Properties()
props.load(new FileInputStream(new File(propertiesFile)))
// map to the bindings
def bindings = [:]
props.propertyNames().each{prop->
bindings[prop]=props.getProperty(prop)
}
// create the template and make the output file
def engine = new XmlTemplateEngine()
def templateFile = new File(templateFileName)
def output = engine.createTemplate(templateFile).make(bindings)
def outputFile = new File(outputFileName)
def parentFile = outputFile.getParentFile()
if (parentFile != null) parentFile.mkdirs()
def fileWriter = new FileWriter(outputFile)
fileWriter.write(output.toString())
fileWriter.close()
}
Java serialzation
class parent implements Serializable {
int parentVersion = 10;
}
class contain implements Serializable{
int containVersion = 11;
}
public class SerialTest extends parent implements Serializable {
int version = 66;
contain con = new contain();
public int getVersion() {
return version;
}
public static void main(String args[]) throws IOException {
FileOutputStream fos = new FileOutputStream("temp.out");
ObjectOutputStream oos = new ObjectOutputStream(fos);
SerialTest st = new SerialTest();
oos.writeObject(st);
oos.flush();
oos.close();
}
}
Boolean fun from eclipse project
public void setEnabled(boolean enabled) {
if (enabled != this.enabled) {
Boolean oldVal = this.enabled ? Boolean.TRUE : Boolean.FALSE;
Boolean newVal = enabled ? Boolean.TRUE : Boolean.FALSE;
this.enabled = enabled;
firePropertyChange(ENABLED, oldVal, newVal);
}
}
Resizing Generic Arrays in Java in A Generic Way
package ch.hsr.ifs.liquid.util;
import static java.lang.System.arraycopy;
import java.lang.reflect.Array;
public class Arrays {
private static final int ENLARGE_FACTOR = 2;
public static <T> T[] enlargeArray(T[] array) {
T[] enlargedArray = initArray(array, array.length * ENLARGE_FACTOR);
arraycopy(array, 0, enlargedArray, 0, array.length);
return enlargedArray;
}
public static <T> T[] trimArray(T[] array, int length) {
T[] trimmedArray = initArray(array, length);
arraycopy(array, 0, trimmedArray, 0, length);
return trimmedArray;
}
public static <T> T[] composeArray(T[] array1, T[] array2) {
int length1 = array1.length;
int length2 = array2.length;
T[] composedArray = initArray(array1, length1 + length2);
arraycopy(array1, 0, composedArray, 0, length1);
arraycopy(array2, 0, composedArray, length1, length2);
return composedArray;
}
@SuppressWarnings("unchecked")
private static <T> T[] initArray(T[] array, int newSize) {
return (T[]) Array.newInstance(array.getClass().getComponentType(), newSize);
}
}
C3P0 DataSource Config (Connection-Pool)
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClassName}"></property>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="initialPoolSize" value="5"></property>
<property name="minPoolSize" value="5"></property>
<property name="maxPoolSize" value="15"/>
<property name="checkoutTimeout" value="1000"/> <!-- Wartezeit auf freie Pool-Connection -->
<property name="maxStatements" value="50"></property>
<property name="automaticTestTable" value="C3P0_TEST_TABLE"></property>
<property name="testConnectionOnCheckin" value="true"></property>
<property name="idleConnectionTestPeriod" value="60"></property> <!-- in Sekunden -->
</bean>
Java jtable methods
tblTable.setAutoCreateRowSorter(true); //will make the colums sortable tblTable.setCellSelectionEnabled(false); //will diable the selection of single cells tblTable.setRowSelectionAllowed(true); //set it so that when you click a row, the entire row will be selected tblTable.setColumnSelectionAllowed(false); //set it so that you can't select entire columns tblTable.getTableHeader().setReorderingAllowed(false); //disable the changing of column positions
Get the time in defined format
/**
* Get the time in defined format
*
* @return String with time
*/
public static String getTime() {
String DATE_FORMAT_NOW = "HH:mm:ss";
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT_NOW);
return sdf.format(cal.getTime());
}
Trim the file path from the given file name.
/**
* Trim the file path from the given file name. Anything before the last occurred "/" and "" will be
* trimmed, including the slash.
*
* @param fileName
* The file name to trim the file path from.
* @return The file name with the file path trimmed.
*/
public static String trimFilePath(String fileName) {
return fileName.substring(fileName.lastIndexOf("/") + 1).substring(fileName.lastIndexOf("") + 1);
}