krysalis-update/src/java/org/krysalis/depot/update/protocols package.html,NONE,1.1 ProtocolOperationsManager.java,NONE,1.1 DefaultProtocolOperationsManager.java,NONE,1.1 Protocol.java,NONE,1.1 IProtocolOperationsProvider.java,NONE,1.1

Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:06:36 +0000
Newsgroups gmane.comp.krysalis.cvs
Message-ID <[email protected]>
Update of /cvsroot/krysalis/krysalis-update/src/java/org/krysalis/depot/update/protocols
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/java/org/krysalis/depot/update/protocols

Added Files:
	package.html ProtocolOperationsManager.java 
	DefaultProtocolOperationsManager.java Protocol.java 
	IProtocolOperationsProvider.java 
Log Message:
Copied from the apache incubator.

--- NEW FILE: package.html ---
<body>
<p>Depot File Handling 'Sub-System'<br>
	Re-usable components for downloading, copying, extracting...</p>
</body>

--- NEW FILE: IProtocolOperationsProvider.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.update.protocols;

import java.util.List;

import org.krysalis.depot.update.impl.ArtifactUpdaterContext;
import org.krysalis.depot.update.util.net.VirtualResourceLocator;
import org.krysalis.depot.update.util.select.ISelector;

/**
 * @author arb_jack
 */
public interface IProtocolOperationsProvider {

	//	Configuration...
	void init(ArtifactUpdaterContext context) throws Exception ;
	List getSupportedSourceProtocols();
	List getSupportedDestinationProtocols();

	//	Work...
	boolean exists(VirtualResourceLocator container) throws Exception;
	List list(VirtualResourceLocator container) throws Exception;
	List list(VirtualResourceLocator container, ISelector selector) throws Exception;
	void copy(VirtualResourceLocator from, VirtualResourceLocator to) throws Exception;
	void copy(VirtualResourceLocator from, VirtualResourceLocator to, ISelector selector) throws Exception;
	void delete(VirtualResourceLocator locator, ISelector selector) throws Exception;
}

--- NEW FILE: Protocol.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.update.protocols;

/**
 * @author ajack
 */
public class Protocol {

	public final static String HTTP_PROTOCOL = "http";
	public final static String FILE_PROTOCOL = "file";

	public static boolean isHTTP(String protocol) {
		return HTTP_PROTOCOL.equalsIgnoreCase(protocol);
	}

	public static boolean isFile(String protocol) {
		return FILE_PROTOCOL.equalsIgnoreCase(protocol);
	}
}

--- NEW FILE: DefaultProtocolOperationsManager.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.update.protocols;

import org.krysalis.depot.common.log.Logger;
import org.krysalis.depot.common.util.debug.DebugUtils;
import org.krysalis.depot.common.util.envsafe.ClassLoaderContext;
import org.krysalis.depot.update.UpdateRuntimeException;
import org.krysalis.depot.update.impl.ArtifactUpdaterContext;

/**
 * @author ajack
 */
public class DefaultProtocolOperationsManager extends ProtocolOperationsManager {

	public static final String VFS_PROVIDER = "org.krysalis.depot.update.protocols.impl.VfsProtocolOperationsProvider";
	public static final String HTTP_CLIENT_PROVIDER = "org.krysalis.depot.update.protocols.impl.HttpClientProtocolOperationsProvider";
	public static final String URL_PROVIDER = "org.krysalis.depot.update.protocols.impl.BasicProtocolOperationsProvider";

	/**
	 * @param context
	 */
	public DefaultProtocolOperationsManager(ArtifactUpdaterContext context) {
		super(context);

		// Shoot for VFS, but fallback to HttpClient or basic URL
		try {
			ClassLoaderContext loader = new ClassLoaderContext(context);

			IProtocolOperationsProvider provider = (IProtocolOperationsProvider) loader.loadClassWithFallbacks(
					VFS_PROVIDER, HTTP_CLIENT_PROVIDER, URL_PROVIDER);

			registerProvider(provider);
			
			Logger.getLogger().info("Selected Provider: " + provider.getClass().getName() + " for " + context);
			
		} catch (Exception ve) {
			throw new UpdateRuntimeException(
					"No *Working* Protocol Operation Providers Detected", ve);
		}		
	}

	public static void main(String args[]) {
		ProtocolOperationsManager pom = new DefaultProtocolOperationsManager(
				ArtifactUpdaterContext.getTestContext());

		DebugUtils.dump(pom);
	}
}

--- NEW FILE: ProtocolOperationsManager.java ---
/*
 * Copyright  2004 The Apache Software Foundation
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.krysalis.depot.update.protocols;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.krysalis.depot.common.log.Logger;
import org.krysalis.depot.common.util.debug.DebugUtils;
import org.krysalis.depot.common.util.debug.Dumpable;
import org.krysalis.depot.update.UpdateException;
import org.krysalis.depot.update.impl.ArtifactUpdaterContext;
import org.krysalis.depot.update.util.net.VirtualResourceLocator;
import org.krysalis.depot.update.util.select.ISelector;
import org.krysalis.depot.update.util.select.SelectionHelper;

/**
 * @author ajack
 */
public class ProtocolOperationsManager implements Dumpable {
	private ArtifactUpdaterContext m_context = null;
	private List m_providers = new ArrayList();

	private Map m_sourceProtocolMap = new HashMap();
	private Map m_destinationProtocolMap = new HashMap();

	private class SourceProtocolProviderSelector implements ISelector {

		String m_sourceProtocol;

		public SourceProtocolProviderSelector(String protocol) {
			m_sourceProtocol = protocol;
		}

		public boolean select(Object protocolProvider) {

			List listOfSourceProtocols = ((IProtocolOperationsProvider) protocolProvider).getSupportedSourceProtocols();

			for (Iterator protocolIterator = listOfSourceProtocols.iterator(); protocolIterator.hasNext();) {
				String protocol = (String) protocolIterator.next();
				if (protocol.equals(m_sourceProtocol)) {
					return true;
				}
			}
			return false;
		}
	}

	private class DestinationProtocolProviderSelector implements ISelector {

		String m_destinationProtocol;

		public DestinationProtocolProviderSelector(String protocol) {
			m_destinationProtocol = protocol;
		}

		public boolean select(Object protocolProvider) {

			List listOfDestinationProtocols = ((IProtocolOperationsProvider) protocolProvider).getSupportedDestinationProtocols();

			for (Iterator protocolIterator = listOfDestinationProtocols.iterator(); protocolIterator.hasNext();) {
				String protocol = (String) protocolIterator.next();
				if (protocol.equals(m_destinationProtocol)) {
					return true;
				}
			}
			return false;
		}
	}

	private class SourceDestinationProtocolProviderSelector
			implements
				ISelector {

		String m_sourceProtocol;
		String m_destinationProtocol;

		public SourceDestinationProtocolProviderSelector(String srcProtocol,
				String destProtocol) {
			m_sourceProtocol = srcProtocol;
			m_destinationProtocol = destProtocol;
		}

		public boolean select(Object protocolProvider) {

			List listOfSourceProtocols = ((IProtocolOperationsProvider) protocolProvider).getSupportedSourceProtocols();

			// check the src protocol
			for (Iterator srcProtocolIterator = listOfSourceProtocols.iterator(); srcProtocolIterator.hasNext();) {
				String protocol = (String) srcProtocolIterator.next();
				if (protocol.equals(m_sourceProtocol)) {
					// if src protocol matches, check the destination protocol
					List listOfDestinationProtocols = ((IProtocolOperationsProvider) protocolProvider).getSupportedDestinationProtocols();

					for (Iterator protocolIterator = listOfDestinationProtocols.iterator(); protocolIterator.hasNext();) {
						String destProtocol = (String) protocolIterator.next();
						if (destProtocol.equals(m_destinationProtocol)) {
							return true;
						}
					}
				}
			}
			return false;
		}

	}

	ProtocolOperationsManager(ArtifactUpdaterContext context) {
		m_context = context;
	}

	public void registerProvider(IProtocolOperationsProvider p)
			throws Exception {
		m_providers.add(p);
		p.init(m_context);

		registerIntoMap(m_sourceProtocolMap, p,
				p.getSupportedSourceProtocols().iterator());

		registerIntoMap(m_destinationProtocolMap, p,
				p.getSupportedDestinationProtocols().iterator());
	}

	public Set getSupportedSourceProtocols() {
		return m_sourceProtocolMap.keySet();
	}

	public Set getSupportedDestinationProtocols() {
		return m_destinationProtocolMap.keySet();
	}

	private void registerIntoMap(Map map, IProtocolOperationsProvider p,
			Iterator i) {
		for (; i.hasNext();) {
			addProvider(map, (String) i.next(), p);
		}
	}

	public boolean hasProviders() {
		return !m_providers.isEmpty();
	}

	/**
	 * Copy the contents at address to another
	 * 
	 * @param from
	 * @param to
	 * @throws Exception
	 */
	public List performList(VirtualResourceLocator container, ISelector selector)
			throws Exception {
		return performList(container, determineProviderFor(container), selector);
	}

	public List performList(VirtualResourceLocator container,
			IProtocolOperationsProvider provider, ISelector selector)
			throws Exception {
		Logger.getLogger().debug("Perform LIST: " + container);
		return provider.list(container, selector);
	}

	/**
	 * Copy the contents at address to another
	 * 
	 * @param from
	 * @param to
	 * @throws Exception
	 */
	public boolean checkExists(VirtualResourceLocator container)
			throws Exception {
		Logger.getLogger().debug("Check EXISTS: " + container);
		boolean exists = determineProviderFor(container).exists(container);
		return exists;
	}

	/**
	 * Copy the contents at address to another
	 * 
	 * @param from
	 * @param to
	 * @throws Exception
	 */
	public void performCopy(VirtualResourceLocator from,
			VirtualResourceLocator to, ISelector selector) throws Exception {
		Logger.getLogger().info("Perform COPY " + from + " -> " + to);
		determineProviderFor(from, to).copy(from, to, selector);
	}

	/**
	 * Copy the contents at address to another
	 * 
	 * @param from
	 * @param to
	 * @throws Exception
	 */
	public void performDelete(VirtualResourceLocator locator, ISelector selector)
			throws Exception {
		Logger.getLogger().info("Perform DELETE " + locator);
		determineProviderFor(locator).delete(locator, selector);
	}

	IProtocolOperationsProvider determineProviderFor(
			VirtualResourceLocator source, VirtualResourceLocator destination)
			throws UpdateException {
		// Locate a Provider that handles both address
		// types (e.g. protocols)

		List providers = determineSourceDestinationProviders(
				source.getProtocol(), destination.getProtocol());

		if (0 == providers.size())
			throw new UpdateException("No providers for protocols : "
					+ source.getProtocol() + " and "
					+ destination.getProtocol());

		IProtocolOperationsProvider provider = (IProtocolOperationsProvider) providers.get(0);

		//
		// :TODO: Maybe filter/select by capabilities.
		//

		return provider;
	}

	IProtocolOperationsProvider determineProviderFor(
			VirtualResourceLocator source) throws UpdateException {
		// Locate a Provider that handles both address
		// types (e.g. protocols)
		List providers = determineSourceProviders(source.getProtocol());

		if (null == providers)
			throw new UpdateException("No providers for protocol : "
					+ source.getProtocol());

		IProtocolOperationsProvider provider = (IProtocolOperationsProvider) providers.get(0);

		//
		// :TODO: Maybe filter/select by capabilities.
		//

		return provider;
	}

	private List determineSourceProviders(String protocol)
			throws UpdateException {
		return SelectionHelper.select(m_providers, new SourceProtocolProviderSelector(
				protocol));
	}

	private List determineDestinationProviders(String protocol)
			throws UpdateException {
		return SelectionHelper.select(m_providers, new DestinationProtocolProviderSelector(
				protocol));
	}

	private List determineSourceDestinationProviders(String srcProtocol,
			String destProtocol) throws UpdateException {
		return SelectionHelper.select(m_providers, new SourceDestinationProtocolProviderSelector(
				srcProtocol, destProtocol));
	}

	private void addProvider(Map map, String protocol,
			IProtocolOperationsProvider p) {
		List providers = (List) map.get(protocol);
		if (null == providers) {
			providers = new ArrayList();
			map.put(protocol, providers);
		}
		providers.add(p);
	}

	/**
	 * Show the contents of this ProtocolManager
	 */
	public void dump(PrintWriter out, int depth, boolean verbose) {
		//String indent = DebugUtils.getIndent(depth);
		String indent1 = DebugUtils.getIndent(depth + 1);

		dumpRegistry(out, indent1, "Source Protocols", m_sourceProtocolMap);

		dumpRegistry(out, indent1, "Destination Protocols",
				m_destinationProtocolMap);
	}

	private void dumpRegistry(PrintWriter out, String indent, String title,
			Map map) {

		out.print(indent);
		out.println("Registry: " + title);

		for (Iterator i = map.keySet().iterator(); i.hasNext();) {
			Object key = i.next();
			List providers = (List) map.get(key);

			for (Iterator ii = providers.iterator(); ii.hasNext();) {
				IProtocolOperationsProvider p = (IProtocolOperationsProvider) ii.next();
				out.print(indent);
				out.print("Key: \'" + key + "\' -> ");
				out.println("Provider: " + p);
			}
		}
	}
}


-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/