Tag Archive for java

Hibernate XML Mapping

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="eg">

<class
name="Blog"
table="BLOGS">

<id
name="id"
column="BLOG_ID">

<generator class="native"/>

</id>

<property
name="name"
column="NAME"
not-null="true"
unique="true"/>

<bag
name="items"
inverse="true"
order-by="DATE_TIME"
cascade="all">

<key column="BLOG_ID"/>
<one-to-many class="BlogItem"/>

</bag>

</class>

</hibernate-mapping>

source

Context file for Tomcat

<?xml version='1.0' encoding='utf-8'?>
<Context docBase="/path/to/youProjectDir/web"
path="/youProjectURL"
reloadable="true"
workDir="/path/to/youProjectDir/work">

<!-- Contents -->

</Context>

source

Serialization of Java object to AMF

package amfdemo;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;

import flex.messaging.io.SerializationContext;
import flex.messaging.io.amf.Amf3Input;
import flex.messaging.io.amf.Amf3Output;
import javax.xml.parsers.DocumentBuilderFactory;

public class AmfTest {

public static void main(String[] args) {
try {

SerializationContext context = getSerializationContext();

TestBean testBean = new TestBean();
testBean.setByte(new Byte((byte) 9));
testBean.setBigDecimal(new BigDecimal("9.9"));
testBean.setBoolean(new Boolean("true"));
testBean.setCharacter(new Character('c'));
testBean.setCalendar(Calendar.getInstance());
testBean.setDate(Calendar.getInstance().getTime());
testBean.setDouble(new Double(999.9));
testBean.setFloat(new Float(99.9f));
testBean.setInteger(new Integer("999"));
testBean.setList(new ArrayList());
testBean.setLong(new Long(99999));
testBean.setMap(new HashMap());
testBean.setDocument(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
testBean.setShort(new Short("99"));
testBean.setString("test");

ByteArrayOutputStream bout = new ByteArrayOutputStream();
Amf3Output amf3Output = new Amf3Output(context);
amf3Output.setOutputStream(bout);
amf3Output.writeObject(testBean);
amf3Output.flush();
amf3Output.close();

InputStream bIn = new ByteArrayInputStream(bout.toByteArray());
Amf3Input amf3Input = new Amf3Input(context);
amf3Input.setInputStream(bIn);
TestBean o = (TestBean) amf3Input.readObject();

System.out.println(o.getByte().equals(testBean.getByte()));
System.out.println(o.getBigDecimal().equals(testBean.getBigDecimal()));
System.out.println(o.getBoolean().equals(testBean.getBoolean()));
System.out.println(o.getCharacter().equals(testBean.getCharacter()));
System.out.println(o.getCalendar().equals(testBean.getCalendar()));
System.out.println(o.getDate().equals(testBean.getDate()));
System.out.println(o.getDouble().equals(testBean.getDouble()));
System.out.println(o.getFloat().equals(testBean.getFloat()));
System.out.println(o.getInteger().equals(testBean.getInteger()));
System.out.println(o.getLong().equals(testBean.getLong()));
System.out.println(o.getString().equals(testBean.getString()));

} catch (Exception e) {
e.printStackTrace();
}
}

public static SerializationContext getSerializationContext() {

//Threadlocal SerializationContent
SerializationContext serializationContext = SerializationContext.getSerializationContext();
serializationContext.enableSmallMessages = true;
serializationContext.instantiateTypes = true;
//use _remoteClass field
serializationContext.supportRemoteClass = true;
//false  Legacy Flex 1.5 behavior was to return a java.util.Collection for Array
//ture New Flex 2+ behavior is to return Object[] for AS3 Array
serializationContext.legacyCollection = false;

serializationContext.legacyMap = false;
//false Legacy flash.xml.XMLDocument Type
//true New E4X XML Type
serializationContext.legacyXMLDocument = false;

//determines whether the constructed Document is name-space aware
serializationContext.legacyXMLNamespaces = false;
serializationContext.legacyThrowable = false;
serializationContext.legacyBigNumbers = false;

serializationContext.restoreReferences = false;
serializationContext.logPropertyErrors = false;
serializationContext.ignorePropertyErrors = true;
return serializationContext;

/*
serializationContext.enableSmallMessages = serialization.getPropertyAsBoolean(ENABLE_SMALL_MESSAGES, true);
serializationContext.instantiateTypes = serialization.getPropertyAsBoolean(INSTANTIATE_TYPES, true);
serializationContext.supportRemoteClass = serialization.getPropertyAsBoolean(SUPPORT_REMOTE_CLASS, false);
serializationContext.legacyCollection = serialization.getPropertyAsBoolean(LEGACY_COLLECTION, false);
serializationContext.legacyMap = serialization.getPropertyAsBoolean(LEGACY_MAP, false);
serializationContext.legacyXMLDocument = serialization.getPropertyAsBoolean(LEGACY_XML, false);
serializationContext.legacyXMLNamespaces = serialization.getPropertyAsBoolean(LEGACY_XML_NAMESPACES, false);
serializationContext.legacyThrowable = serialization.getPropertyAsBoolean(LEGACY_THROWABLE, false);
serializationContext.legacyBigNumbers = serialization.getPropertyAsBoolean(LEGACY_BIG_NUMBERS, false);
boolean showStacktraces = serialization.getPropertyAsBoolean(SHOW_STACKTRACES, false);
if (showStacktraces && Log.isWarn())
log.warn("The " + SHOW_STACKTRACES + " configuration option is deprecated and non-functional. Please remove this from your configuration file.");
serializationContext.restoreReferences = serialization.getPropertyAsBoolean(RESTORE_REFERENCES, false);
serializationContext.logPropertyErrors = serialization.getPropertyAsBoolean(LOG_PROPERTY_ERRORS, false);
serializationContext.ignorePropertyErrors = serialization.getPropertyAsBoolean(IGNORE_PROPERTY_ERRORS, true);
*/
}
}

source

OAF – Controller Code to create Javascript popup window

String pubOrderId = pageContext.getParameter("orderId");
StringBuffer l_buffer = new StringBuffer();
StringBuffer l_buffer1 = new StringBuffer();
l_buffer.append("javascript:mywin = openWindow(top, '");
l_buffer1.append("/jct/oracle/apps/xxpwc/entry/webui/AddAttachmentPG");
l_buffer1.append("&retainAM=Y");
l_buffer1.append("&pubOrderId="+pubOrderId);
String url = "/OA_HTML/OA.jsp?page="+l_buffer1.toString();
OAUrl popupUrl = new OAUrl(url, OAWebBeanConstants.ADD_BREAD_CRUMB_SAVE );
String strUrl = popupUrl.createURL(pageContext);
l_buffer.append(strUrl.toString());
l_buffer.append("', 'lovWindow', {width:750, height:550},false,'dialog',null);");
pageContext.putJavaScriptFunction("SomeName",l_buffer.toString());

source

Convert java properties file from UTF to ASCII

native2ascii -encoding UnicodeBig app_zh.ucd app_zh.properties

source

Create a JavaBean

jar cfm BangBean.jar BangBean.mf bangbean

source

Java Collection utilities

public class CollectionInitUtils {

public static <T> T[] ar(final T... ts) {
return ts;
}

public static <T> Set<T> set(final T... ts) {
return new HashSet<T>(Arrays.asList(ts));
}

public static <K, V> Map<K, V> zipMap(final K[] keys, final V[] values) {
final Map<K, V> res = new HashMap<K, V>();
for (int i = 0; i < keys.length; i++) {
res.put(keys[i], values[i]);
}
return res;
}
}

source

Install and use SVNANT

<path id="project.classpath">
<pathelement location="${basedir}/lib/svnjavahl.jar" />
<pathelement location="${basedir}/lib/svnant.jar" />
<pathelement location="${basedir}/lib/svnClientAdapter.jar" />
</path>

<taskdef resource="svntask.properties" classpathref="project.classpath"/>

<target name="checkout">
<svn></svn>
</target>

source

Accessing windows registry in Java

import java.lang.reflect.Method;
import java.util.prefs.Preferences;

public class JavaRegistryHack {

private static final int HKEY_CURRENT_USER = 0x80000001;
private static final int KEY_QUERY_VALUE = 1;
private static final int KEY_SET_VALUE = 2;
private static final int KEY_READ = 0x20019;

public static void main(String args[]) {
final Preferences userRoot = Preferences.userRoot();
final Preferences systemRoot = Preferences.systemRoot();
final Class clz = userRoot.getClass();
try {
final Method openKey = clz.getDeclaredMethod("openKey",
byte[].class, int.class, int.class);
openKey.setAccessible(true);

final Method closeKey = clz.getDeclaredMethod("closeKey",
int.class);
closeKey.setAccessible(true);

final Method winRegQueryValue = clz.getDeclaredMethod(
"WindowsRegQueryValueEx", int.class, byte[].class);
winRegQueryValue.setAccessible(true);
final Method winRegEnumValue = clz.getDeclaredMethod(
"WindowsRegEnumValue1", int.class, int.class, int.class);
winRegEnumValue.setAccessible(true);
final Method winRegQueryInfo = clz.getDeclaredMethod(
"WindowsRegQueryInfoKey1", int.class);
winRegQueryInfo.setAccessible(true);

byte[] valb = null;
String vals = null;
String key = null;
Integer handle = -1;

//Query Internet Settings for Proxy
key = "SoftwareMicrosoftWindowsCurrentVersionInternet Settings";
handle = (Integer) openKey.invoke(userRoot, toCstr(key), KEY_READ, KEY_READ);
valb = (byte[]) winRegQueryValue.invoke(userRoot, handle.intValue(),
toCstr("ProxyServer"));
vals = (valb != null ? new String(valb).trim() : null);
System.out.println("Proxy Server = " + vals);
closeKey.invoke(Preferences.userRoot(), handle);

// Query for IE version
key = "SOFTWAREMicrosoftInternet Explorer";
handle = (Integer) openKey.invoke(systemRoot, toCstr(key), KEY_READ, KEY_READ);
valb = (byte[]) winRegQueryValue.invoke(systemRoot, handle, toCstr("Version"));
vals = (valb != null ? new String(valb).trim() : null);
System.out.println("Internet Explorer Version = " + vals);
closeKey.invoke(Preferences.systemRoot(), handle);

} catch (Exception e) {
e.printStackTrace();
}
}

private static byte[] toCstr(String str) {
byte[] result = new byte[str.length() + 1];
for (int i = 0; i < str.length(); i++) {
result[i] = (byte) str.charAt(i);
}
result[str.length()] = 0;
return result;
}

source

JavaScript ShortCut Keys

function ShortCutKey()
{
//User hit Ctrl+S
if (window.event.keyCode == 19)
window.location="search.html";
//User hit Ctrl+Q
if (window.event.keyCode == 81)
window.location="quick-search.html"
}

source