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.camel.component.jcr;
018    
019    import java.net.URI;
020    import java.net.URISyntaxException;
021    
022    import javax.jcr.Credentials;
023    import javax.jcr.Repository;
024    import javax.jcr.SimpleCredentials;
025    
026    import org.apache.camel.Consumer;
027    import org.apache.camel.Processor;
028    import org.apache.camel.Producer;
029    import org.apache.camel.RuntimeCamelException;
030    import org.apache.camel.impl.DefaultEndpoint;
031    import org.apache.camel.impl.DefaultExchange;
032    
033    /**
034     * A JCR endpoint
035     */
036    public class JcrEndpoint extends DefaultEndpoint<DefaultExchange> {
037    
038        private Credentials credentials;
039        private Repository repository;
040        private String base;
041    
042        @SuppressWarnings("unchecked")
043        protected JcrEndpoint(String endpointUri, JcrComponent component) {
044            super(endpointUri, component);
045            try {
046                URI uri = new URI(endpointUri);
047                if (uri.getUserInfo() != null && uri.getAuthority() != null) {
048                    this.credentials = new SimpleCredentials(uri.getUserInfo(), uri.getAuthority().toCharArray());
049                }
050                this.repository = (Repository) component.getCamelContext().getRegistry().lookup(uri.getHost());
051                if (repository == null) {
052                    throw new RuntimeCamelException("No JCR repository defined under '" + uri.getHost() + "'");
053                }
054                this.base = uri.getPath().replaceAll("^/", "");
055            } catch (URISyntaxException e) {
056                throw new IllegalArgumentException("Invalid URI: " + endpointUri, e);
057            }
058        }
059    
060        public JcrEndpoint(String endpointUri, String base, Credentials credentials, Repository repository) {
061            super(endpointUri);
062            this.base = base;
063            this.credentials = credentials;
064            this.repository = repository;
065        }
066    
067        /**
068         * Currently unsupported
069         * @throws RuntimeCamelException
070         */
071        public Consumer<DefaultExchange> createConsumer(Processor processor) throws Exception {
072            throw new RuntimeCamelException("No consumer endpoint support for JCR available");
073        }
074    
075        /**
076         * Creates a new {@link Producer} 
077         */
078        public Producer<DefaultExchange> createProducer() throws Exception {
079            return new JcrProducer(this);
080        }
081    
082        /**
083         * {@inheritDoc}
084         */
085        public boolean isSingleton() {
086            return false;
087        }
088    
089        /**
090         * Get the {@link Repository}
091         * 
092         * @return the repository
093         */
094        protected Repository getRepository() {
095            return repository;
096        }
097    
098        /**
099         * Get the {@link Credentials} for establishing the JCR repository connection
100         * 
101         * @return the credentials
102         */
103        protected Credentials getCredentials() {
104            return credentials;
105        }
106    
107        /**
108         * Get the base node when accessing the reposititory
109         * 
110         * @return the base node
111         */
112        protected String getBase() {
113            return base;
114        }
115    
116    }