- added ProtocolAction which allows the transferring of a file from one site to another site (using different Protocols if necessary -------------- OODT-194 git-svn-id: https://svn.apache.org/repos/asf/oodt/branches/protocol@1137031 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/protocol-api/src/main/java/org/apache/oodt/cas/protocol/action/CrossProtocolTransferAction.java b/protocol-api/src/main/java/org/apache/oodt/cas/protocol/action/CrossProtocolTransferAction.java new file mode 100644 index 0000000..559a361 --- /dev/null +++ b/protocol-api/src/main/java/org/apache/oodt/cas/protocol/action/CrossProtocolTransferAction.java
@@ -0,0 +1,106 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.oodt.cas.protocol.action; + +//JDK imports +import java.io.File; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.UUID; + +//OODT imports +import org.apache.oodt.cas.protocol.Protocol; +import org.apache.oodt.cas.protocol.ProtocolFile; +import org.apache.oodt.cas.protocol.auth.Authentication; +import org.apache.oodt.cas.protocol.auth.BasicAuthentication; +import org.apache.oodt.cas.protocol.auth.NoAuthentication; +import org.apache.oodt.cas.protocol.system.ProtocolManager; +import org.apache.oodt.cas.protocol.verify.ProtocolVerifier; +import org.apache.oodt.cas.protocol.verify.ProtocolVerifierFactory; + +/** + * {@link ProtocolAction} for transferring a file from one host to another + * + * @author bfoster + */ +public class CrossProtocolTransferAction extends ProtocolAction { + + private URI fromUri; + private URI toUri; + private ProtocolVerifierFactory verifierFactory; + + @Override + public void performAction(ProtocolManager protocolManager) throws Exception { + ProtocolVerifier verifier = null; + if (verifierFactory != null) { + verifier = verifierFactory.newInstance(); + } + + File localFile = createTempDownloadFile(); + if (localFile == null) { + throw new Exception("Failed to create tempory local file"); + } + + Protocol fromProtocol = protocolManager.getProtocolBySite(fromUri, getAuthentication(fromUri), verifier); + if (fromProtocol == null) { + throw new Exception("Failed to get protocol for 'from' URI '" + fromUri + "'"); + } + + Protocol toProtocol = protocolManager.getProtocolBySite(toUri, getAuthentication(toUri), verifier); + if (toProtocol == null) { + throw new Exception("Failed to get protocol for 'to' URI '" + toUri + "'"); + } + + fromProtocol.get(new ProtocolFile(fromUri.getPath(), false), localFile); + toProtocol.put(localFile, new ProtocolFile(toUri.getPath(), false)); + } + + public void setFromUri(String fromUri) throws URISyntaxException { + this.fromUri = new URI(fromUri); + } + + public void setToUri(String toUri) throws URISyntaxException { + this.toUri = new URI(toUri); + } + + public void setVerifierFactory(ProtocolVerifierFactory verifierFactory) { + this.verifierFactory = verifierFactory; + } + + private File createTempDownloadFile() { + File bogusFile = null; + try { + bogusFile = File.createTempFile("bogus", "bogus"); + File tmpDir = new File(bogusFile.getParentFile(), "ProtocolTransfer/" + UUID.randomUUID().toString()); + tmpDir.mkdirs(); + return new File(tmpDir, "temp_file"); + } catch (Exception e) { + return null; + } finally { + try { bogusFile.delete(); } catch (Exception e) {} + } + } + + private Authentication getAuthentication(URI uri) { + if (uri.getUserInfo() != null) { + String[] userInfo = uri.getUserInfo().split("\\:"); + return new BasicAuthentication(userInfo[0], userInfo[1]); + } else { + return new NoAuthentication(); + } + } +}
diff --git a/protocol-api/src/main/java/org/apache/oodt/cas/protocol/verify/BasicProtocolVerifierFactory.java b/protocol-api/src/main/java/org/apache/oodt/cas/protocol/verify/BasicProtocolVerifierFactory.java new file mode 100644 index 0000000..c7fa6ef --- /dev/null +++ b/protocol-api/src/main/java/org/apache/oodt/cas/protocol/verify/BasicProtocolVerifierFactory.java
@@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You 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.apache.oodt.cas.protocol.verify; + +//JDK imports +import java.net.URI; +import java.util.HashMap; +import java.util.Map; + +//OODT imports +import org.apache.oodt.cas.protocol.ProtocolFile; + +/** + * {@link ProtocolVerifierFactory} which creates {@link BasicProtocolVerifier}. + * + * @author bfoster + */ +public class BasicProtocolVerifierFactory implements ProtocolVerifierFactory { + + private Map<URI, ProtocolFile> testCdMap; + + public ProtocolVerifier newInstance() { + if (testCdMap != null) { + return new BasicProtocolVerifier(testCdMap); + } else { + return new BasicProtocolVerifier(new HashMap<URI, ProtocolFile>()); + } + } + + public void setTestCdMap(Map<URI, ProtocolFile> testCdMap) { + this.testCdMap = testCdMap; + } +}
diff --git a/protocol-api/src/main/resources/policy/protocol-action-beans.xml b/protocol-api/src/main/resources/policy/protocol-action-beans.xml index 15fc7fb..e3c32d5 100644 --- a/protocol-api/src/main/resources/policy/protocol-action-beans.xml +++ b/protocol-api/src/main/resources/policy/protocol-action-beans.xml
@@ -13,6 +13,11 @@ <bean class="org.apache.oodt.commons.spring.postprocessor.SetIdBeanPostProcessor"/> <bean id="Download" class="org.apache.oodt.cas.protocol.action.DownloadAction"/> - <bean id="GetSupportedFactories" class="org.apache.oodt.cas.protocol.action.GetSupportedFactoriesAction"/> + <bean id="GetSupportedFactories" class="org.apache.oodt.cas.protocol.action.GetSupportedFactoriesAction"/> + <bean id="CrossProtocolTransfer" class="org.apache.oodt.cas.protocol.action.CrossProtocolTransferAction"> + <property name="verifierFactory" ref="BasicVerifier"/> + </bean> + + <bean id="BasicVerifier" class="org.apache.oodt.cas.protocol.verify.BasicProtocolVerifierFactory"/> </beans> \ No newline at end of file
diff --git a/protocol-api/src/main/resources/policy/protocol-cmd-line-beans.xml b/protocol-api/src/main/resources/policy/protocol-cmd-line-beans.xml index 91bb9b8..6cf0e5c 100644 --- a/protocol-api/src/main/resources/policy/protocol-cmd-line-beans.xml +++ b/protocol-api/src/main/resources/policy/protocol-cmd-line-beans.xml
@@ -1,78 +1,149 @@ <?xml version="1.0" encoding="UTF-8"?> -<!-- - - Author: bfoster - Description: ProtocolManager Configuration - ---> +<!-- Author: bfoster Description: ProtocolManager Configuration --> <beans xmlns="http://www.springframework.org/schema/beans" - xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" - xmlns:p="http://www.springframework.org/schema/p" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> - <bean class="org.apache.oodt.commons.spring.postprocessor.SetIdBeanPostProcessor"/> + <bean + class="org.apache.oodt.commons.spring.postprocessor.SetIdBeanPostProcessor" /> - <bean id="action" lazy-init="true" class="org.apache.oodt.commons.option.CmdLineOption"> - <property name="shortOption" value="a"/> - <property name="longOption" value="action"/> - <property name="description" value="The Action to perfrom"/> - <property name="hasArgs" value="true"/> - <property name="optionArgName" value="bean id"/> - <property name="required" value="true"/> + <bean id="action" lazy-init="true" + class="org.apache.oodt.commons.option.CmdLineOption"> + <property name="shortOption" value="a" /> + <property name="longOption" value="action" /> + <property name="description" value="The Action to perfrom" /> + <property name="hasArgs" value="true" /> + <property name="optionArgName" value="bean id" /> + <property name="required" value="true" /> </bean> - - <bean id="user" lazy-init="true" class="org.apache.oodt.commons.option.CmdLineOption"> - <property name="shortOption" value="u"/> - <property name="longOption" value="user"/> - <property name="description" value="Username for authentication"/> - <property name="hasArgs" value="true"/> - <property name="optionArgName" value="username"/> - <property name="required" value="false"/> + + <bean id="user" lazy-init="true" + class="org.apache.oodt.commons.option.CmdLineOption"> + <property name="shortOption" value="u" /> + <property name="longOption" value="user" /> + <property name="description" value="Username for authentication" /> + <property name="hasArgs" value="true" /> + <property name="optionArgName" value="username" /> + <property name="required" value="false" /> <property name="handler"> - <bean class="org.apache.oodt.commons.option.handler.CmdLineOptionBeanHandler"> + <bean + class="org.apache.oodt.commons.option.handler.CmdLineOptionBeanHandler"> <property name="applyToBeans"> <list> - <bean class="org.apache.oodt.commons.option.handler.BeanInfo" p:bean-ref="Download"/> + <bean class="org.apache.oodt.commons.option.handler.BeanInfo" + p:bean-ref="Download" /> </list> </property> </bean> </property> </bean> - - <bean id="pass" lazy-init="true" class="org.apache.oodt.commons.option.CmdLineOption"> - <property name="shortOption" value="p"/> - <property name="longOption" value="pass"/> - <property name="description" value="Password for authentication"/> - <property name="hasArgs" value="true"/> - <property name="optionArgName" value="password"/> - <property name="required" value="false"/> + + <bean id="pass" lazy-init="true" + class="org.apache.oodt.commons.option.CmdLineOption"> + <property name="shortOption" value="p" /> + <property name="longOption" value="pass" /> + <property name="description" value="Password for authentication" /> + <property name="hasArgs" value="true" /> + <property name="optionArgName" value="password" /> + <property name="required" value="false" /> <property name="handler"> - <bean class="org.apache.oodt.commons.option.handler.CmdLineOptionBeanHandler"> + <bean + class="org.apache.oodt.commons.option.handler.CmdLineOptionBeanHandler"> <property name="applyToBeans"> <list> - <bean class="org.apache.oodt.commons.option.handler.BeanInfo" p:bean-ref="Download"/> + <bean class="org.apache.oodt.commons.option.handler.BeanInfo" + p:bean-ref="Download" /> </list> </property> </bean> </property> </bean> - - <bean id="url" lazy-init="true" class="org.apache.oodt.commons.option.CmdLineOption"> - <property name="shortOption" value="url"/> - <property name="longOption" value="url"/> - <property name="description" value="URL to download"/> - <property name="hasArgs" value="true"/> - <property name="optionArgName" value="URL"/> - <property name="required" value="false"/> + + <bean id="url" lazy-init="true" + class="org.apache.oodt.commons.option.CmdLineOption"> + <property name="shortOption" value="url" /> + <property name="longOption" value="url" /> + <property name="description" value="URL to download" /> + <property name="hasArgs" value="true" /> + <property name="optionArgName" value="URL" /> + <property name="required" value="false" /> <property name="handler"> - <bean class="org.apache.oodt.commons.option.handler.CmdLineOptionBeanHandler"> + <bean + class="org.apache.oodt.commons.option.handler.CmdLineOptionBeanHandler"> <property name="applyToBeans"> <list> - <bean class="org.apache.oodt.commons.option.handler.BeanInfo" p:bean-ref="Download"/> + <bean class="org.apache.oodt.commons.option.handler.BeanInfo" + p:bean-ref="Download" /> </list> </property> </bean> </property> </bean> - + + <bean id="fromUrl" lazy-init="true" + class="org.apache.oodt.commons.option.CmdLineOption"> + <property name="shortOption" value="fu" /> + <property name="longOption" value="fromUrl" /> + <property name="description" value="URL to get file" /> + <property name="hasArgs" value="true" /> + <property name="optionArgName" value="URL" /> + <property name="requiredOptions"> + <list> + <bean class="org.apache.oodt.commons.option.required.RequiredOption"> + <property name="optionLongName" value="action" /> + <property name="requireAllValues" value="false" /> + <property name="optionValues"> + <list> + <value>CrossProtocolTransfer</value> + </list> + </property> + </bean> + </list> + </property> + <property name="handler"> + <bean + class="org.apache.oodt.commons.option.handler.CmdLineOptionBeanHandler"> + <property name="applyToBeans"> + <list> + <bean class="org.apache.oodt.commons.option.handler.BeanInfo" + p:bean-ref="CrossProtocolTransfer" p:methodName="setFromUri" /> + </list> + </property> + </bean> + </property> + </bean> + + <bean id="toUrl" lazy-init="true" + class="org.apache.oodt.commons.option.CmdLineOption"> + <property name="shortOption" value="tu" /> + <property name="longOption" value="toUrl" /> + <property name="description" value="URL to send file" /> + <property name="hasArgs" value="true" /> + <property name="optionArgName" value="URL" /> + <property name="requiredOptions"> + <list> + <bean class="org.apache.oodt.commons.option.required.RequiredOption"> + <property name="optionLongName" value="action" /> + <property name="requireAllValues" value="false" /> + <property name="optionValues"> + <list> + <value>CrossProtocolTransfer</value> + </list> + </property> + </bean> + </list> + </property> + <property name="handler"> + <bean + class="org.apache.oodt.commons.option.handler.CmdLineOptionBeanHandler"> + <property name="applyToBeans"> + <list> + <bean class="org.apache.oodt.commons.option.handler.BeanInfo" + p:bean-ref="CrossProtocolTransfer" p:methodName="setToUri" /> + </list> + </property> + </bean> + </property> + </bean> + </beans> \ No newline at end of file
diff --git a/protocol-api/src/main/resources/policy/protocol-config.xml b/protocol-api/src/main/resources/policy/protocol-config.xml index 50f838d..0adb860 100644 --- a/protocol-api/src/main/resources/policy/protocol-config.xml +++ b/protocol-api/src/main/resources/policy/protocol-config.xml
@@ -12,6 +12,6 @@ <import resource="protocol-action-beans.xml"/> <import resource="protocol-cmd-line-beans.xml"/> - <import resource="classpath*:**/*-protocol-config.xml"/> + <import resource="classpath*:/org/apache/oodt/cas/protocol/**/*-protocol-config.xml"/> </beans> \ No newline at end of file