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