001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.servicemix.truezip;
018    
019    // import java.io.File;
020    import java.net.URI;
021    import java.util.List;
022    import java.util.Map;
023    
024    import javax.jbi.servicedesc.ServiceEndpoint;
025    
026    import org.apache.servicemix.common.DefaultComponent;
027    import org.apache.servicemix.common.Endpoint;
028    import org.apache.servicemix.jbi.util.IntrospectionSupport;
029    import org.apache.servicemix.jbi.util.URISupport;
030    
031    import de.schlichtherle.io.File;
032    
033    /**
034     * A file based component
035     * 
036     * @version $Revision: 10098 $
037     * @org.apache.xbean.XBean element="component" description="File Component"
038     */
039    public class TrueZipComponent extends DefaultComponent {
040    
041        private TrueZipEndpointType[] endpoints;
042    
043        public TrueZipEndpointType[] getEndpoints() {
044            return endpoints;
045        }
046    
047        public void setEndpoints(TrueZipEndpointType[] endpoints) {
048            this.endpoints = endpoints;
049        }
050    
051        protected List getConfiguredEndpoints() {
052            return asList(getEndpoints());
053        }
054    
055        protected Class[] getEndpointClasses() {
056            return new Class[] { TrueZipPollerEndpoint.class, TrueZipSenderEndpoint.class };
057        }
058    
059        protected Endpoint getResolvedEPR(ServiceEndpoint ep) throws Exception {
060            // We receive an exchange for an EPR that has not been used yet.
061            // Register a provider endpoint and restart processing.
062            TrueZipSenderEndpoint fileEp = new TrueZipSenderEndpoint(this, ep);
063            URI uri = new URI(ep.getEndpointName());
064            String file = null;
065            String host = uri.getHost();
066            String path = uri.getPath();
067            if (host != null) {
068                if (path != null) {
069                    // lets assume host really is a relative directory
070                    file = host + File.separator + path;
071                } else {
072                    file = host;
073                }
074            } else {
075                if (path != null) {
076                    file = path;
077                } else {
078                    // must be an absolute URI
079                    file = uri.getSchemeSpecificPart();
080                }
081            }
082            Map map = URISupport.parseQuery(uri.getQuery());
083            if (IntrospectionSupport.setProperties(fileEp, map, "truezip.")) {
084                uri = URISupport.createRemainingURI(uri, map);
085            }
086            if (file != null) {
087                fileEp.setDirectory(new File(file));
088            } else {
089                throw new IllegalArgumentException("No file defined for URL: " + uri);
090            }
091            fileEp.activate();
092            return fileEp;
093        }
094    
095    }