org.cauldron.execution.ContextUtils.java Source code

Java tutorial

Introduction

Here is the source code for org.cauldron.execution.ContextUtils.java

Source

/*
 * Copyright (c) 2004 - 2006, Jonathan Ross <jonross@alum.mit.edu>
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

package org.cauldron.execution;

import java.util.Collection;
import java.util.Map;
import java.util.Set;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import org.springframework.beans.factory.BeanFactory;

import org.cauldron.Context;
import org.cauldron.Task;

/*
 * Utility classes used by ContextImpl. In a separate file for manageability.
 */

class ContextUtils {
}

/*
 * Common interface for task configuration sources.
 */

interface TaskConfig {
    Task find(String name);
}

/*
 * Spring-based task finder.
 */

class SpringTaskConfig implements TaskConfig {
    private BeanFactory bf;

    SpringTaskConfig(BeanFactory bf) {
        this.bf = bf;
    }

    public Task find(String name) {
        return (Task) bf.getBean(name);
    }
}

/*
 * JNDI-based task finder.
 */

class JNDITaskConfig implements TaskConfig {
    private String prefix;

    JNDITaskConfig(String prefix) {
        this.prefix = prefix;
    }

    public Task find(String name) {
        try {
            InitialContext ic = new InitialContext();
            return (Task) ic.lookup(prefix + name);
        } catch (NamingException e) {
            ContextImpl.log.error("JNDI exception while finding task named " + name + " : " + e.getMessage());
            return null;
        }
    }
}

/*
 * A Map wrapper around a Context. Only supports the get() method, for text
 * substitution.
 */

class ContextMap implements Map {
    private Context context;

    ContextMap(Context context) {
        this.context = context;
    }

    public int size() {
        throw new UnsupportedOperationException("Operation not supported on Context");
    }

    public void clear() {
        throw new UnsupportedOperationException("Operation not supported on Context");
    }

    public boolean isEmpty() {
        throw new UnsupportedOperationException("Operation not supported on Context");
    }

    public boolean containsKey(Object arg0) {
        throw new UnsupportedOperationException("Operation not supported on Context");
    }

    public boolean containsValue(Object arg0) {
        throw new UnsupportedOperationException("Operation not supported on Context");
    }

    public Collection values() {
        throw new UnsupportedOperationException("Operation not supported on Context");
    }

    public void putAll(Map arg0) {
        throw new UnsupportedOperationException("Operation not supported on Context");
    }

    public Set entrySet() {
        throw new UnsupportedOperationException("Operation not supported on Context");
    }

    public Set keySet() {
        throw new UnsupportedOperationException("Operation not supported on Context");
    }

    public Object get(Object arg0) {
        return context.get(arg0.toString());
    }

    public Object remove(Object arg0) {
        throw new UnsupportedOperationException("Operation not supported on Context");
    }

    public Object put(Object arg0, Object arg1) {
        throw new UnsupportedOperationException("Operation not supported on Context");
    }
}