Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Carl Leiby leibys-place.com

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

Dynamic Select Example

// Example of JavaScript to with with Select Options

<html>
<head>
	<title>test</title>	
</head>
<body id="test" onload="">
	<form>
		<select id="lstStuff" multiple="multiple" onChange="lstStuff_OnChange()" size="6" style="width:200px;">
			<option>item 1</option>
			<option>item 2</option>
			<option>item 3</option>
			<option>item 4</option>
			<option>item 5</option>
			<option>item 6</option>			
		</select>	
		<br/>
		<a href="javascript:selectAll('lstStuff', true);">all</a>
		<a href="javascript:selectAll('lstStuff', false);">none</a>
		<p/>
		<select id="lstOtherStuff" multiple="multiple" size="6" style="width:200px;">
		</select>
		<br/>
		<a href="javascript:selectAll('lstOtherStuff', true);">all</a>
		<a href="javascript:selectAll('lstOtherStuff', false);">none</a>
	</form>
	<script type="text/javascript" charset="utf-8">
		var otherStuff = {
			"item 1" : [ "subitem 1.1", "subitem 1.2", "subitem 1.3", "subitem 1.4" ],
			"item 2" : [ "subitem 2.1", "subitem 2.2" ],
			"item 4" : [ "subitem 4" ],
			"item 6" : [ "subitem 6.1", "subitem 6.2" ]
		};
	</script>
	<script type="text/javascript" charset="utf-8">
		function selectAll(listName, selected) {
			var listBox = document.getElementById(listName);
			for(i=0; i<listBox.length; i++) {
				listBox.options[i].selected=selected;
			}
			if( listBox.onchange ) {
				listBox.onchange();
			}
		}
		function lstStuff_OnChange() {
			var listBox = document.getElementById("lstStuff");
			var subListBox = document.getElementById("lstOtherStuff");
			subListBox.options.length=0;
			for(i=0; i<listBox.length; i++) {
				if( listBox.options[i].selected ) {
					var key = listBox.options[i].text;
					if(otherStuff[key]) {
						for(j=0; j<otherStuff[key].length; j++) {
							subListBox.options.add(new Option(otherStuff[key][j],otherStuff[key][j]));
						}
					}
				}
			}
		}
	</script>
</body>
</html>

Making a local hash based Context

// This may be total overkill, but I wanted to use a DataSource in a stand alone app.

jndi.properties
java.naming.factory.initial=com.admin.model.naming.ContextFactory
java.naming.provider.url=iiop://localhost:1050


ContextFactory.java
package com.admin.model.naming;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.spi.InitialContextFactory;
public class ContextFactory implements InitialContextFactory {
    public ContextFactory() {
    }
    public Context getInitialContext(Hashtable<?, ?> environment) throws NamingException {
        ContextImpl ctx = (ContextImpl)ContextImpl.getInstance();
        ctx.setEnvironment(environment);
        return ctx;
    }
}


ContextImpl.java
package com.admin.model.naming;
import java.util.Hashtable;
import java.util.logging.Logger;
import javax.naming.Binding;
import javax.naming.CompositeName;
import javax.naming.Context;
import javax.naming.Name;
import javax.naming.NameClassPair;
import javax.naming.NameParser;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
public class ContextImpl implements Context {
    private static Logger logger = Logger.getLogger(ContextImpl.class.getName());
    private static ContextImpl instance = new ContextImpl();
    private Hashtable environment;
    private Hashtable<Name, Object> directory = new Hashtable();

    private ContextImpl() {
    }

    static Context getInstance() {
        return instance;
    }
    
    public Object lookup(Name name) throws NamingException {
        logger.entering(getClass().getName(), "lookup", name);
        Object result = null;        
        if(!directory.containsKey(name)) {
            throw new NamingException("Naming directory does not contain entry for: " + name);
        }
        result = directory.get(name);
        logger.exiting(getClass().getName(), "lookup", result);
        return result;
    }

    public Object lookup(String name) throws NamingException {
        logger.entering(getClass().getName(), "lookup", name);
        Object result = null;     
        Name properName = new CompositeName(name);
        if(!directory.containsKey(properName)) {
            throw new NamingException("Naming directory does not contain entry for: " + properName);
        }
        result = directory.get(properName);
        logger.exiting(getClass().getName(), "lookup", result);
        return result;
    }

    public void bind(Name name, Object obj) throws NamingException {
        logger.entering(getClass().getName(), "bind", new Object[]{name, obj});
        if(directory.containsKey(name)) {
            throw new NamingException("Naming directory already contains entry for: " + name);
        }
        directory.put(name, obj);
    }

    public void bind(String name, Object obj) throws NamingException {
        logger.entering(getClass().getName(), "bind", new Object[]{name, obj});
        Name properName = new CompositeName(name);
        if(directory.containsKey(properName)) {
            throw new NamingException("Naming directory already contains entry for: " + properName);
        }
        directory.put(properName, obj);
    }

    public void rebind(Name name, Object obj) throws NamingException {
        logger.entering(getClass().getName(), "rebind", new Object[]{name, obj});
        if(!directory.containsKey(name)) {
            throw new NamingException("Naming directory does not contain entry for: " + name);
        }
        directory.put(name, obj);
    }

    public void rebind(String name, Object obj) throws NamingException {
        logger.entering(getClass().getName(), "rebind", new Object[]{name, obj});
        Name properName = new CompositeName(name);
        if(!directory.containsKey(properName)) {
            throw new NamingException("Naming directory does not contain entry for: " + properName);
        }
        directory.put(properName, obj);
    }

    public void unbind(Name name) throws NamingException {
        logger.entering(getClass().getName(), "unbind", name);
        if(!directory.containsKey(name)) {
            throw new NamingException("Naming directory does not contain entry for: " + name);
        }
        directory.remove(name);
    }

    public void unbind(String name) throws NamingException {
        logger.entering(getClass().getName(), "unbind", name);
        Name properName = new CompositeName(name);
        if(!directory.containsKey(properName)) {
            throw new NamingException("Naming directory does not contain entry for: " + properName);
        }
        directory.remove(properName);
    }

    public void rename(Name oldName, Name newName) throws NamingException {
        logger.entering(getClass().getName(), "rename", new Object[]{oldName, newName});
        Object obj = lookup(oldName);
        unbind(oldName);
        bind(newName, obj);
    }

    public void rename(String oldName, String newName) throws NamingException {
        logger.entering(getClass().getName(), "rename", new Object[]{oldName, newName});
        Object obj = lookup(oldName);
        unbind(oldName);
        bind(newName, obj);
    }

    public NamingEnumeration<NameClassPair> list(Name name) throws NamingException {
        logger.entering(getClass().getName(), "list", name);
        return null;
    }

    public NamingEnumeration<NameClassPair> list(String name) throws NamingException {
        logger.entering(getClass().getName(), "list", name);
        return null;
    }

    public NamingEnumeration<Binding> listBindings(Name name) throws NamingException {
        logger.entering(getClass().getName(), "listBindings", name);
        return null;
    }

    public NamingEnumeration<Binding> listBindings(String name) throws NamingException {
        logger.entering(getClass().getName(), "listBindings", name);
        return null;
    }

    public void destroySubcontext(Name name) throws NamingException {
        logger.entering(getClass().getName(), "destroySubcontext", name);
    }

    public void destroySubcontext(String name) throws NamingException {
        logger.entering(getClass().getName(), "destroySubcontext", name);
    }

    public Context createSubcontext(Name name) throws NamingException {
        logger.entering(getClass().getName(), "createSubcontext", name);
        return null;
    }

    public Context createSubcontext(String name) throws NamingException {
        logger.entering(getClass().getName(), "createSubcontext", name);
        return null;
    }

    public Object lookupLink(Name name) throws NamingException {
        logger.entering(getClass().getName(), "lookupLink", name);
        return null;
    }

    public Object lookupLink(String name) throws NamingException {
        logger.entering(getClass().getName(), "lookupLink", name);
        return null;
    }

    public NameParser getNameParser(Name name) throws NamingException {
        logger.entering(getClass().getName(), "getNameParser", name);
        return null;
    }

    public NameParser getNameParser(String name) throws NamingException {
        logger.entering(getClass().getName(), "getNameParser", name);
        return null;
    }

    public Name composeName(Name name, Name prefix) throws NamingException {
        logger.entering(getClass().getName(), "composeName", new Object[]{name, prefix});
        return null;
    }

    public String composeName(String name, String prefix) throws NamingException {
        logger.entering(getClass().getName(), "composeName", new Object[]{name, prefix});
        return null;
    }

    public Object addToEnvironment(String propName, Object propVal) throws NamingException {
        logger.entering(getClass().getName(), "addToEnvironment", new Object[]{propName, propVal});
        environment.put(propName, propVal);
        return null;
    }

    public Object removeFromEnvironment(String propName) throws NamingException {
        logger.entering(getClass().getName(), "removeFromEnvironment", propName);
        Object result = environment.get(propName);
        environment.remove(propName);
        return result;
    }

    public Hashtable getEnvironment() throws NamingException {
        logger.entering(getClass().getName(), "getEnvironment");
        return environment;
    }

    void setEnvironment(Hashtable env) {
        environment = env;
    }
    
    public void close() throws NamingException {
        logger.entering(getClass().getName(), "close");
    }

    public String getNameInNamespace() throws NamingException {
        logger.entering(getClass().getName(), "getNameInNamespace");
        return null;
    }
}


usage
InitialContext ctx = new InitialContext();
ctx.bind("DataSource", ds);

Loading an xml properties file

// simple example of loading an xml file into Properties.

Properties properties = new Properties();
try {
    InputStream xmlStream = getClass().getResourceAsStream("properties.xml");
    if( xmlStream == null ) {
         //throw some error
    }            
    properties.loadFromXML(xmlStream);
} catch (IOException exception) {
    // throw exception
}


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
    <entry key="plugin.TestPlugin">com.plugin.test.TestPluginModule</entry>
    <entry key="messages.TestPlugin">TestPluginMessages.properties</entry>
</properties>
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS