krysalis-update/src/java/org/krysalis/depot/update/protocols/impl BasicProtocolOperationsProvider.java,NONE,1.1 HttpClientProtocolOperationsProvider.java,NONE,1.1 AbstractProtocolOperationsProvider.java,NONE,1.1 VfsProtocolOperationsProvider.java,NONE,1.1 FileProtocolOperationsProvider.java,NONE,1.1
Nick Chalko <[email protected]> Wed, 08 Dec 2004 09:06:40 +0000
| Newsgroups | gmane.comp.krysalis.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/krysalis/krysalis-update/src/java/org/krysalis/depot/update/protocols/impl
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24726/src/java/org/krysalis/depot/update/protocols/impl
Added Files:
BasicProtocolOperationsProvider.java
HttpClientProtocolOperationsProvider.java
AbstractProtocolOperationsProvider.java
VfsProtocolOperationsProvider.java
FileProtocolOperationsProvider.java
Log Message:
Copied from the apache incubator.
--- NEW FILE: FileProtocolOperationsProvider.java ---
/*
* Copyright 2000-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.impl;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.krysalis.depot.update.UpdateException;
import org.krysalis.depot.update.impl.ArtifactUpdaterContext;
import org.krysalis.depot.update.protocols.Protocol;
import org.krysalis.depot.update.util.io.FileUtils;
import org.krysalis.depot.update.util.io.ResolvedFile;
import org.krysalis.depot.update.util.net.VirtualResourceLocator;
import org.krysalis.depot.update.util.select.ISelector;
import org.krysalis.depot.common.log.Logger;
/**
* @author ajack
*/
public class FileProtocolOperationsProvider
extends
AbstractProtocolOperationsProvider {
public void init(ArtifactUpdaterContext context) throws Exception {
super.init(context);
addSourceProtocol(Protocol.FILE_PROTOCOL);
addDestinationProtocol(Protocol.FILE_PROTOCOL);
}
public boolean exists(VirtualResourceLocator container) throws Exception {
return container.getFile().exists();
}
public List list(VirtualResourceLocator container, ISelector selector)
throws Exception {
List children = new ArrayList();
File folder = container.getFile();
if (!folder.isDirectory())
throw new UpdateException("Not a directory : " + folder);
File files[] = folder.listFiles();
if (null != files)
for (int i = 0; i < files.length; ++i) {
File file = files[i];
children.add(new VirtualResourceLocator(ResolvedFile.resolve(
folder, file.getName())));
}
return children;
}
public void copy(VirtualResourceLocator from, VirtualResourceLocator to,
ISelector selector) throws Exception {
File toFile = to.getFile();
// Build Folders to this Folder
toFile.getParentFile().mkdirs();
try {
FileUtils.transfer(from.getFile(), toFile);
} finally { // Attempt at cleanup
try {
toFile.delete();
} catch (Exception ee) {
}
Logger.getLogger().error(
"Failed to copy from: " + from + " to " + to);
}
}
public void delete(VirtualResourceLocator locator, ISelector selector)
throws Exception {
File file = locator.getFile();
boolean deleted = file.delete();
if (!deleted)
Logger.getLogger().error("Failed to delete file: " + file);
}
}
--- NEW FILE: VfsProtocolOperationsProvider.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.impl;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemManager;
import org.krysalis.depot.update.impl.ArtifactUpdaterContext;
import org.krysalis.depot.update.protocols.Protocol;
import org.krysalis.depot.update.util.io.VfsUtils;
import org.krysalis.depot.update.util.net.VirtualResourceLocator;
import org.krysalis.depot.update.util.net.select.ChildVrlSelector;
import org.krysalis.depot.update.util.select.ISelector;
/**
* @author ajack
*/
public class VfsProtocolOperationsProvider
extends AbstractProtocolOperationsProvider {
private FileSystemManager m_fsm = null;
public VfsProtocolOperationsProvider() {
// Force an early fail, not on in init, so can trap it
// and try another provider
Class causeAFailedLoadAtConstructionIfGoingToHappen = FileSystemManager.class;
Class causeAFailedLoadAtConstructionIfGoingToHappen2 = LogFactory.class;
}
public void init(ArtifactUpdaterContext context) throws Exception {
super.init(context);
addSourceProtocol(Protocol.HTTP_PROTOCOL);
addSourceProtocol(Protocol.FILE_PROTOCOL);
addDestinationProtocol(Protocol.HTTP_PROTOCOL);
addDestinationProtocol(Protocol.FILE_PROTOCOL);
m_fsm =
VfsUtils.createFSM(
context.getIdentifier().getId(),
context.getBaseFile().getAbsolutePath());
//:TODO: Get Protocols from FSM..
}
public boolean exists(VirtualResourceLocator container) throws Exception {
FileObject fileObject = resolveAddress(container);
return fileObject.exists();
}
public List list(VirtualResourceLocator container, ISelector selector)
throws Exception {
//:TODO: MOVE TO GET LINKS w/ selector -- ASAP
List fileObjects = VfsUtils.getChildren(resolveAddress(container));
// Convert the list of File Objects to Locators...
List locators = new ArrayList(fileObjects.size());
ISelector childSelector = new ChildVrlSelector(container);
for (Iterator i = fileObjects.iterator(); i.hasNext();) {
FileObject fileObject = (FileObject) i.next();
VirtualResourceLocator vrl = resolveLocator(fileObject);
if (childSelector.select(vrl))
locators.add(vrl);
}
return locators;
}
public void copy(
VirtualResourceLocator from,
VirtualResourceLocator to,
ISelector selector)
throws Exception {
FileObject fromFile = resolveAddress(from);
FileObject toFile = resolveAddress(to);
// Build Folders to this Folder
buildPathTo(toFile.getParent());
// Copy in contents..
VfsUtils.copyFrom(toFile,fromFile);
}
public void delete(
VirtualResourceLocator locator,
ISelector selector)
throws Exception {
FileObject file = resolveAddress(locator);
// Remove file [if protocol allows]
VfsUtils.delete(file);
}
private void buildPathTo(FileObject container) throws Exception {
FileObject parent = container.getParent();
// Recurse up to try to make parent tree..
if (!parent.exists()) {
buildPathTo(parent);
}
// Now, assuming parent is ok, make this..
if (!container.exists()) {
container.createFolder();
}
}
/**
* Resolve an address into a VFS FileObject
*
* @param address
* @return
* @throws Exception
*/
private FileObject resolveAddress(VirtualResourceLocator address)
throws Exception {
return m_fsm.resolveFile(address.toExternalForm());
}
/**
* Resolve a VFS FileObject into an address
*
* @param fileObject
* @return
* @throws Exception
*/
private VirtualResourceLocator resolveLocator(FileObject fileObject)
throws Exception {
return new VirtualResourceLocator(fileObject.getURL());
}
}
--- NEW FILE: BasicProtocolOperationsProvider.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.impl;
import java.net.URL;
import java.util.List;
import org.krysalis.depot.update.impl.ArtifactUpdaterContext;
import org.krysalis.depot.update.protocols.Protocol;
import org.krysalis.depot.update.util.net.VirtualResourceLocator;
import org.krysalis.depot.update.util.select.ISelector;
/**
* @author ajack
*/
public class BasicProtocolOperationsProvider
extends AbstractProtocolOperationsProvider {
public void init(ArtifactUpdaterContext context) throws Exception {
super.init(context);
// We can read files or HTTP
addSourceProtocol(Protocol.HTTP_PROTOCOL);
addSourceProtocol(Protocol.FILE_PROTOCOL);
// We only write to files
addDestinationProtocol(Protocol.FILE_PROTOCOL);
}
public boolean exists(VirtualResourceLocator container)
throws Exception {
boolean exists = false;
URL url = container.getURL();
// :TODO: Get URL contents and list
if (true) throw new UnsupportedOperationException();
return exists;
}
public List list(VirtualResourceLocator container, ISelector selector)
throws Exception {
List results = null;
URL url = container.getURL();
// :TODO: Get URL contents and list
if (true) throw new UnsupportedOperationException();
return results;
}
public void copy(
VirtualResourceLocator from,
VirtualResourceLocator to,
ISelector selector)
throws Exception {
URL url1 = from.getURL();
URL url2 = to.getURL();
// :TODO: Get URL contents and write to a file...
if (true) throw new UnsupportedOperationException();
}
public void delete(
VirtualResourceLocator locator,
ISelector selector)
throws Exception {
URL url = locator.getURL();
// :TODO: Delete...
if (true) throw new UnsupportedOperationException();
}
}
--- NEW FILE: AbstractProtocolOperationsProvider.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.impl;
import java.util.ArrayList;
import java.util.List;
import org.krysalis.depot.update.impl.ArtifactUpdaterContext;
import org.krysalis.depot.update.protocols.IProtocolOperationsProvider;
import org.krysalis.depot.update.util.net.VirtualResourceLocator;
import org.krysalis.depot.update.util.select.AllSelector;
/**
* @author ajack
*/
public abstract class AbstractProtocolOperationsProvider
implements IProtocolOperationsProvider {
protected ArtifactUpdaterContext m_context = null;
protected List m_sourceProtocols = new ArrayList();
protected List m_destinationProtocols = new ArrayList();
AbstractProtocolOperationsProvider() {
}
void addSourceProtocol(String protocol) {
m_sourceProtocols.add(protocol);
}
public List getSupportedSourceProtocols() {
return m_sourceProtocols;
}
void addDestinationProtocol(String protocol) {
m_destinationProtocols.add(protocol);
}
public List getSupportedDestinationProtocols() {
return m_destinationProtocols;
}
public void init(ArtifactUpdaterContext context) throws Exception {
m_context = context;
}
public List list(VirtualResourceLocator container) throws Exception {
return list(container, AllSelector.getInstance());
}
public void copy(VirtualResourceLocator from, VirtualResourceLocator to) throws Exception {
copy(from, to, AllSelector.getInstance());
}
}
--- NEW FILE: HttpClientProtocolOperationsProvider.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.impl;
import java.beans.Encoder;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.logging.LogFactory;
import org.krysalis.depot.update.impl.ArtifactUpdaterContext;
import org.krysalis.depot.update.protocols.Protocol;
import org.krysalis.depot.update.util.io.HtmlUtils;
import org.krysalis.depot.update.util.net.VirtualResourceLocator;
import org.krysalis.depot.update.util.net.http.HttpClientUserAgent;
import org.krysalis.depot.update.util.select.ISelector;
import org.krysalis.depot.common.util.io.IOUtils;
/**
*
* Implments an HTTP provider (ontop of File) so can do http:// file://
*
* @author ajack
*/
public class HttpClientProtocolOperationsProvider
extends FileProtocolOperationsProvider {
private HttpClientUserAgent m_agent = null;
public HttpClientProtocolOperationsProvider() {
// Force an early fail, not on in init, so can trap it
// and try another provider
Class causeAFailedLoadAtConstructionIfGoingToHappen = HttpClientUserAgent.class;
Class causeAFailedLoadAtConstructionIfGoingToHappen2 = LogFactory.class;
Class causeAFailedLoadAtConstructionIfGoingToHappen3 = HttpMethod.class;
Class causeAFailedLoadAtConstructionIfGoingToHappen4 = Encoder.class;
}
public void init(ArtifactUpdaterContext context) throws Exception {
super.init(context);
addSourceProtocol(Protocol.HTTP_PROTOCOL);
addDestinationProtocol(Protocol.HTTP_PROTOCOL);
m_agent = new HttpClientUserAgent();
}
/**
* Check if the VRL exists
*/
public boolean exists(VirtualResourceLocator container) throws Exception {
boolean exists = false;
if (Protocol.isHTTP(container.getProtocol()))
exists = m_agent.exists(container.toExternalForm());
else
exists = super.exists(container);
return exists;
}
/**
* List the contents of the VRL
*/
public List list(VirtualResourceLocator container, ISelector selector)
throws Exception {
List children = null;
if (Protocol.isHTTP(container.getProtocol())) {
children =
HtmlUtils.getLinks(
container,
m_agent.getContents(container.toExternalForm()),
selector);
}
else
children = super.list(container, selector);
return children;
}
/**
* Copy from one (remote) location to another (local) location.
*/
public void copy(
VirtualResourceLocator from,
VirtualResourceLocator to,
ISelector selector)
throws Exception {
File toFile = to.getFile();
// Build Folders to this Folder
toFile.getParentFile().mkdirs();
try {
if (Protocol.isHTTP(from.getProtocol())) { // Copy data...
FileOutputStream stream = null;
try {
stream = new FileOutputStream(toFile);
m_agent.transferContents(from.toExternalForm(), stream);
}
finally {
IOUtils.close(stream);
}
}
else
super.copy(from, to, selector);
}
finally { // Attempt at cleanup
try {
toFile.delete();
}
catch (Exception ee) {
}
}
}
}
-------------------------------------------------------
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/