activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From chir...@apache.org
Subject svn commit: r523078 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/component/pojo/ main/java/org/apache/camel/component/pojo/timer/ main/java/org/apache/camel/component/queue/ main/java/org/apache/camel/util/ main/resources/META-...
Date Tue, 27 Mar 2007 21:03:52 GMT
Author: chirino
Date: Tue Mar 27 14:03:50 2007
New Revision: 523078

URL: http://svn.apache.org/viewvc?view=rev&rev=523078
Log:
Refactor of the PollingPojoComponent.



Added:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerComponent.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerConsumer.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerEndpoint.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerEndpointResolver.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java
    activemq/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/EndpointResolver/timer
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/pojo/timer/
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/pojo/timer/TimerRouteTest.java
Removed:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingPojoComponent.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PollingSchedule.java
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PojoEndpointResolver.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/queue/QueueEndpointResolver.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PojoEndpointResolver.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PojoEndpointResolver.java?view=diff&rev=523078&r1=523077&r2=523078
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PojoEndpointResolver.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/PojoEndpointResolver.java
Tue Mar 27 14:03:50 2007
@@ -53,9 +53,15 @@
 	public PojoEndpoint resolveEndpoint(CamelContext container, String uri) {
 		String id[] = getEndpointId(uri);        
     	PojoComponent component = resolvePojoComponent(container, id[0]);
-        Object pojo = component.getService(id[1]);
-		return new PojoEndpoint(uri, id[1], component);
+		return createEndpoint(uri, id[1], component);
     }
+
+	/**
+	 * PojoEndpointResolver subclasses can override to provide a custom PojoEndpoint implementation.
+	 */
+	protected PojoEndpoint createEndpoint(String uri, String pojoId, PojoComponent component)
{
+		return new PojoEndpoint(uri, pojoId, component);
+	}
 
 	private PojoComponent resolvePojoComponent(CamelContext container, String componentName)
{
     	Component rc = container.getComponent(componentName);

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerComponent.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerComponent.java?view=auto&rev=523078
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerComponent.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerComponent.java
Tue Mar 27 14:03:50 2007
@@ -0,0 +1,50 @@
+/*
+ * 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.camel.component.pojo.timer;
+
+import java.util.ArrayList;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Component;
+import org.apache.camel.component.pojo.PojoExchange;
+
+/**
+ * Represents the component that manages {@link TimerEndpoint}.  It holds the
+ * list of {@link TimerConsumer} objects that are started.
+ * 
+ * @version $Revision: 519973 $
+ */
+public class TimerComponent implements Component<PojoExchange> {
+	
+    protected final ArrayList<TimerConsumer> timers = new ArrayList<TimerConsumer>();
+    
+    private CamelContext container;
+
+    boolean addConsumer(TimerConsumer consumer) {
+        return timers.add(consumer);
+    }
+    boolean removeConsumer(TimerConsumer consumer) {
+        return timers.remove(consumer);
+    }
+
+    public void setCamelContext(CamelContext container) {
+        this.container = container;
+    }
+    public CamelContext getContainer() {
+        return container;
+    }
+}

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerConsumer.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerConsumer.java?view=auto&rev=523078
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerConsumer.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerConsumer.java
Tue Mar 27 14:03:50 2007
@@ -0,0 +1,117 @@
+/**
+ *
+ * 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.camel.component.pojo.timer;
+
+import java.lang.reflect.InvocationHandler;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.lang.reflect.Proxy;
+import java.util.Timer;
+import java.util.TimerTask;
+
+import org.apache.camel.Processor;
+import org.apache.camel.component.pojo.PojoExchange;
+import org.apache.camel.component.pojo.PojoInvocation;
+import org.apache.camel.impl.DefaultConsumer;
+
+/**
+ * @version $Revision: 523047 $
+ */
+public class TimerConsumer extends DefaultConsumer<PojoExchange> implements InvocationHandler
{
+
+    private final TimerEndpoint endpoint;
+    private Timer timer;
+    
+
+	public TimerConsumer(TimerEndpoint endpoint, Processor<PojoExchange> processor) {
+        super(endpoint, processor);
+		this.endpoint = endpoint;
+    }
+    
+    @Override
+    protected void doStart() throws Exception {
+    	TimerComponent component = endpoint.getComponent();
+    	component.addConsumer(this);    	
+        timer=createTimerAndTask();
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        if(timer!=null){
+            timer.cancel();
+        }
+    	TimerComponent component = endpoint.getComponent();
+    	component.removeConsumer(this);    	
+    }
+    
+    private Timer createTimerAndTask(){
+    	
+    	final Runnable proxy = createProxy();
+        TimerTask task=new TimerTask(){
+            @Override public void run(){
+            	proxy.run();
+            }
+        };
+        
+        Timer result=new Timer(endpoint.getTimerName(),endpoint.isDaemon());
+        if(endpoint.isFixedRate()){
+            if(endpoint.getTime()!=null){
+                result.scheduleAtFixedRate(task,endpoint.getTime(),endpoint.getPeriod());
+            }else{
+                result.scheduleAtFixedRate(task,endpoint.getDelay(),endpoint.getPeriod());
+            }
+        }else{
+            if(endpoint.getTime()!=null){
+                if(endpoint.getPeriod()>=0){
+                    result.schedule(task,endpoint.getTime(),endpoint.getPeriod());
+                }else{
+                    result.schedule(task,endpoint.getTime());
+                }
+            }else{
+                if(endpoint.getPeriod()>=0){
+                    result.schedule(task,endpoint.getDelay(),endpoint.getPeriod());
+                }else{
+                    result.schedule(task,endpoint.getDelay());
+                }
+            }
+        }
+        return result;
+    }
+    
+    /**
+     * Creates a Proxy which generates the inbound PojoExchanges
+     */
+    public Runnable createProxy() {
+        return (Runnable) Proxy.newProxyInstance(Runnable.class.getClassLoader(), new Class[]{Runnable.class},
this);
+    }
+    
+	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
+        if (!isStarted()) {
+            throw new IllegalStateException("The endpoint is not active: " + getEndpoint().getEndpointUri());
+        }
+        PojoInvocation invocation = new PojoInvocation(proxy, method, args);
+        PojoExchange exchange = getEndpoint().createExchange();
+        exchange.setInvocation(invocation);
+        getProcessor().onExchange(exchange);
+        Throwable fault = exchange.getException();
+        if (fault != null) {
+            throw new InvocationTargetException(fault);
+        }
+        return exchange.getOut().getBody();
+	}
+}

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerEndpoint.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerEndpoint.java?view=auto&rev=523078
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerEndpoint.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerEndpoint.java
Tue Mar 27 14:03:50 2007
@@ -0,0 +1,122 @@
+/*
+ * 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.camel.component.pojo.timer;
+
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Date;
+import java.util.Map;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.Processor;
+import org.apache.camel.Producer;
+import org.apache.camel.RuntimeCamelException;
+import org.apache.camel.component.pojo.PojoExchange;
+import org.apache.camel.impl.DefaultEndpoint;
+import org.apache.camel.util.IntrospectionSupport;
+import org.apache.camel.util.URISupport;
+
+/**
+ * Represents a timer endpoint that can generate periodic inbound PojoExchanges. 
+ *
+ * @version $Revision: 519973 $
+ */
+public class TimerEndpoint extends DefaultEndpoint<PojoExchange> {
+	
+    private final TimerComponent component;
+	private final String timerName;
+    private Date time;
+    private long period=-1;
+    private long delay=-1;
+    private boolean fixedRate;
+    private boolean daemon=true;
+
+
+    public TimerEndpoint(String fullURI, String timerPartURI, TimerComponent component) throws
URISyntaxException {
+        super(fullURI, component.getContainer());
+        this.component = component;
+		
+        // Use a URI to extract query so they can be set as properties on the endpoint.
+        URI u = new URI(timerPartURI);
+        Map options = URISupport.parseParamters(u);
+		IntrospectionSupport.setProperties(this, options);
+		this.timerName = u.getPath();
+		
+    }
+
+    public Producer<PojoExchange> createProducer() throws Exception {
+    	throw new RuntimeCamelException("Cannot produce to a TimerEndpoint: "+getEndpointUri());
+    }
+
+    public Consumer<PojoExchange> createConsumer(Processor<PojoExchange> processor)
throws Exception {    	
+    	TimerConsumer consumer = new TimerConsumer(this, processor);
+        return startService(consumer);
+    }
+
+    public PojoExchange createExchange() {
+        return new PojoExchange(getContext());
+    }
+
+	public TimerComponent getComponent() {
+		return component;
+	}
+
+	public String getTimerName() {
+		return timerName;
+	}
+
+	public boolean isDaemon() {
+		return daemon;
+	}
+
+	public void setDaemon(boolean daemon) {
+		this.daemon = daemon;
+	}
+
+	public long getDelay() {
+		return delay;
+	}
+
+	public void setDelay(long delay) {
+		this.delay = delay;
+	}
+
+	public boolean isFixedRate() {
+		return fixedRate;
+	}
+
+	public void setFixedRate(boolean fixedRate) {
+		this.fixedRate = fixedRate;
+	}
+
+	public long getPeriod() {
+		return period;
+	}
+
+	public void setPeriod(long period) {
+		this.period = period;
+	}
+
+	public Date getTime() {
+		return time;
+	}
+
+	public void setTime(Date time) {
+		this.time = time;
+	}
+
+}

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerEndpointResolver.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerEndpointResolver.java?view=auto&rev=523078
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerEndpointResolver.java
(added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/pojo/timer/TimerEndpointResolver.java
Tue Mar 27 14:03:50 2007
@@ -0,0 +1,100 @@
+/*
+ * 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.camel.component.pojo.timer;
+
+import java.net.URISyntaxException;
+import java.util.concurrent.Callable;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.Component;
+import org.apache.camel.EndpointResolver;
+import org.apache.camel.component.pojo.PojoExchange;
+import org.apache.camel.util.ObjectHelper;
+
+/**
+ * An implementation of {@link EndpointResolver} that creates 
+ * {@link TimerEndpoint} objects.  TimerEndpoint objects can only 
+ * be consumed.
+ *
+ * The synatax for a Timer URI looks like:
+ * 
+ * <pre><code>timer:[component:]timer-name?options</code></pre>
+ * 
+ * 
+ * @version $Revision: 519901 $
+ */
+public class TimerEndpointResolver implements EndpointResolver<PojoExchange> {
+
+	public static final String DEFAULT_COMPONENT_NAME = TimerComponent.class.getName();
+		
+	/**
+	 * Finds the {@see QueueComponent} specified by the uri.  If the {@see QueueComponent} 
+	 * object do not exist, it will be created.
+	 * 
+	 * @see org.apache.camel.EndpointResolver#resolveComponent(org.apache.camel.CamelContext,
java.lang.String)
+	 */
+	public Component resolveComponent(CamelContext container, String uri) {
+		String id[] = getEndpointId(uri);        
+		return resolveTimerComponent(container, id[0]);
+	}
+
+	/**
+	 * Finds the {@see QueueEndpoint} specified by the uri.  If the {@see QueueEndpoint} or
it's associated
+	 * {@see QueueComponent} object do not exist, they will be created.
+	 * @throws URISyntaxException 
+	 * 
+	 * @see org.apache.camel.EndpointResolver#resolveEndpoint(org.apache.camel.CamelContext,
java.lang.String)
+	 */
+	public TimerEndpoint resolveEndpoint(CamelContext container, String uri) throws URISyntaxException
{
+		String id[] = getEndpointId(uri);        
+    	TimerComponent component = resolveTimerComponent(container, id[0]);
+		return createEndpoint(uri, id[1], component);
+    }
+
+	/**
+	 * PojoEndpointResolver subclasses can override to provide a custom PojoEndpoint implementation.
+	 * @throws URISyntaxException 
+	 */
+	protected TimerEndpoint createEndpoint(String uri, String pojoId, TimerComponent component)
throws URISyntaxException {
+		return new TimerEndpoint(uri, pojoId, component);
+	}
+
+	private TimerComponent resolveTimerComponent(CamelContext container, String componentName)
{
+    	Component rc = container.getOrCreateComponent(componentName, new Callable<Component>(){
+			public Component call() throws Exception {
+				return new TimerComponent();
+			}
+		});
+    	return (TimerComponent) rc;
+	}
+
+	/**
+	 * @return an array that looks like: [componentName,endpointName] 
+	 */
+	private String[] getEndpointId(String uri) {
+		String rc [] = {DEFAULT_COMPONENT_NAME, null};
+		String splitURI[] = ObjectHelper.splitOnCharacter(uri, ":", 3);        
+    	if( splitURI[2] != null ) {
+    		rc[0] =  splitURI[1];
+    		rc[1] =  splitURI[2];
+    	} else {
+    		rc[1] =  splitURI[1];
+    	}
+		return rc;
+	}
+
+}

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/queue/QueueEndpointResolver.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/queue/QueueEndpointResolver.java?view=diff&rev=523078&r1=523077&r2=523078
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/queue/QueueEndpointResolver.java
(original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/component/queue/QueueEndpointResolver.java
Tue Mar 27 14:03:50 2007
@@ -76,7 +76,7 @@
     		rc[0] =  splitURI[1];
     		rc[1] =  splitURI[2];
     	} else {
-    		rc[0] =  splitURI[1];
+    		rc[1] =  splitURI[1];
     	}
 		return rc;
 	}

Added: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java?view=auto&rev=523078
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java (added)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/URISupport.java Tue
Mar 27 14:03:50 2007
@@ -0,0 +1,316 @@
+/**
+ *
+ * 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.camel.util;
+
+import java.io.UnsupportedEncodingException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URLDecoder;
+import java.net.URLEncoder;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+
+/**
+ * @version $Revision$
+ */
+public class URISupport {
+    
+    public static class CompositeData {
+        String scheme;
+        String path;
+        URI components[];
+        Map parameters;
+        String fragment;
+        public String host;
+        
+        public URI[] getComponents() {
+            return components;
+        }
+        public String getFragment() {
+            return fragment;
+        }
+        public Map getParameters() {
+            return parameters;
+        }
+        public String getScheme() {
+            return scheme;
+        }
+        public String getPath() {
+            return path;
+        }
+        public String getHost() {
+            return host;
+        }
+        
+        public URI toURI() throws URISyntaxException {
+            StringBuffer sb = new StringBuffer();
+            if( scheme!=null ) {
+                sb.append(scheme);
+                sb.append(':');
+            }
+            
+            if( host!=null && host.length()!=0 ) {
+                sb.append(host);
+            } else {
+                sb.append('(');
+                for (int i = 0; i < components.length; i++) {
+                    if( i!=0 )
+                        sb.append(',');
+                    sb.append(components[i].toString());
+                }
+                sb.append(')');
+            }
+            
+            if( path !=null ) {
+                sb.append('/');
+                sb.append(path);
+            }
+            if(!parameters.isEmpty()) {
+                sb.append("?");
+                sb.append(createQueryString(parameters));
+            }
+            if( fragment!=null ) {
+                sb.append("#");
+                sb.append(fragment);
+            }
+            return new URI(sb.toString());
+        }
+    }
+
+    public static Map parseQuery(String uri) throws URISyntaxException{
+        try{
+            Map rc=new HashMap();
+            if(uri!=null){
+                String[] parameters=uri.split("&");
+                for(int i=0;i<parameters.length;i++){
+                    int p=parameters[i].indexOf("=");
+                    if(p>=0){
+                        String name=URLDecoder.decode(parameters[i].substring(0,p),"UTF-8");
+                        String value=URLDecoder.decode(parameters[i].substring(p+1),"UTF-8");
+                        rc.put(name,value);
+                    }else{
+                        rc.put(parameters[i],null);
+                    }
+                }
+            }
+            return rc;
+        }catch(UnsupportedEncodingException e){
+            throw (URISyntaxException) new URISyntaxException(e.toString(),"Invalid encoding").initCause(e);
+        }
+    }
+    
+    public static Map parseParamters(URI uri) throws URISyntaxException {
+        return uri.getQuery()==null ? Collections.EMPTY_MAP : parseQuery(stripPrefix(uri.getQuery(),
"?"));
+    }
+
+    /**
+     * Removes any URI query from the given uri
+     */
+    public static URI removeQuery(URI uri) throws URISyntaxException {
+        return createURIWithQuery(uri, null);
+    }
+
+    /**
+     * Creates a URI with the given query
+     */
+    public static URI createURIWithQuery(URI uri, String query) throws URISyntaxException
{
+        return new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(),
uri.getPath(), query, uri.getFragment());
+    }
+    
+    public static CompositeData parseComposite(URI uri) throws URISyntaxException {
+        
+        CompositeData rc = new CompositeData();
+        rc.scheme = uri.getScheme();
+        String ssp = stripPrefix(uri.getSchemeSpecificPart().trim(), "//").trim();
+
+        parseComposite(uri, rc, ssp);
+        
+        rc.fragment = uri.getFragment();
+        return rc;
+    }
+
+    /**
+     * @param uri
+     * @param rc
+     * @param ssp
+     * @param p
+     * @throws URISyntaxException
+     */
+    private static void parseComposite(URI uri, CompositeData rc, String ssp) throws URISyntaxException
{
+        String componentString;
+        String params;
+        
+        if(!checkParenthesis(ssp)){
+            throw new URISyntaxException(uri.toString(), "Not a matching number of '(' and
')' parenthesis");
+        }
+        
+        int p;
+        int intialParen = ssp.indexOf("(");
+        if( intialParen==0 ) {
+            rc.host = ssp.substring(0, intialParen);
+            p = rc.host.indexOf("/");
+            if( p >= 0 ) {
+                rc.path = rc.host.substring(p);
+                rc.host = rc.host.substring(0,p);
+            }
+            p = ssp.lastIndexOf(")");
+            componentString = ssp.substring(intialParen+1,p);
+            params = ssp.substring(p+1).trim();
+            
+        } else {
+            componentString = ssp;
+            params="";
+        }
+
+        String components[] = splitComponents(componentString);
+        rc.components=new URI[components.length];
+        for (int i = 0; i < components.length; i++) {
+            rc.components[i] = new URI(components[i].trim());
+        }
+        
+        p = params.indexOf("?");
+        if( p >= 0 ) {
+            if( p > 0) {
+                rc.path = stripPrefix(params.substring(0, p), "/");
+            }
+            rc.parameters = parseQuery(params.substring(p+1));
+        } else {
+            if( params.length() > 0 )
+                rc.path = stripPrefix(params, "/");
+            rc.parameters = Collections.EMPTY_MAP;
+        }
+    }
+
+    /**
+     * @param componentString
+     * @return
+     */
+    private static String[] splitComponents(String str) {
+        ArrayList l = new ArrayList();
+        
+        int last=0;
+        int depth = 0;
+        char chars[] = str.toCharArray();
+        for( int i=0; i < chars.length; i ++ ) {
+            switch( chars[i] ) {
+            case '(':
+                depth++;
+                break;
+            case ')':
+                depth--;
+                break;
+            case ',':
+                if( depth == 0 ) {
+                    String s = str.substring(last, i);
+                    l.add(s);
+                    last=i+1;
+                }
+            }
+        }
+        
+        String s = str.substring(last);
+        if( s.length() !=0 )
+            l.add(s);        
+        
+        String rc[] = new String[l.size()];
+        l.toArray(rc);
+        return rc;
+    }
+    
+    public static String stripPrefix(String value, String prefix) {
+        if( value.startsWith(prefix) )
+            return value.substring(prefix.length());
+        return value;
+    }
+    
+    public static URI stripScheme(URI uri) throws URISyntaxException {
+        return new URI(stripPrefix(uri.getSchemeSpecificPart().trim(), "//")); 
+    }
+
+    public static String createQueryString(Map options) throws URISyntaxException {
+        try {
+            if(options.size()>0) {
+                StringBuffer rc = new StringBuffer();
+                boolean first=true;
+                for (Iterator iter = options.keySet().iterator(); iter.hasNext();) {
+                    if( first )
+                        first=false;
+                    else
+                        rc.append("&");
+                                    
+                    String key = (String) iter.next();
+                    String value = (String)options.get(key);
+                    rc.append(URLEncoder.encode(key, "UTF-8"));
+                    rc.append("=");
+                    rc.append(URLEncoder.encode(value, "UTF-8"));
+                }
+                return rc.toString();
+            } else {
+                return "";
+            }
+        } catch (UnsupportedEncodingException e) {
+            throw (URISyntaxException)new URISyntaxException(e.toString(), "Invalid encoding").initCause(e);
+        }
+    }
+
+    /**
+     * Creates a URI from the original URI and the remaining paramaters
+     * @throws URISyntaxException 
+     */
+    public static URI createRemainingURI(URI originalURI, Map params) throws URISyntaxException
{
+        String s = createQueryString(params);
+        if( s.length()==0 )
+            s = null;
+        return createURIWithQuery(originalURI, s);
+    }
+
+    static public URI changeScheme(URI bindAddr, String scheme) throws URISyntaxException
{
+        return new URI(scheme, bindAddr.getUserInfo(), bindAddr.getHost(), bindAddr.getPort(),
bindAddr.getPath(), bindAddr.getQuery(), bindAddr.getFragment());
+    }
+    
+    public static boolean checkParenthesis(String str){
+        boolean result=true;
+        if(str!=null){
+            int open=0;
+            int closed=0;
+            
+            int i=0;
+            while((i=str.indexOf('(',i)) >=0 ){
+                i++;
+                open++;
+            }
+            i=0;
+            while((i=str.indexOf(')',i)) >=0 ){
+                i++;
+                closed++;
+            }
+            result = open == closed;
+        }
+        return result;
+    }
+    
+    public int indexOfParenthesisMatch(String str){
+        int result = -1;
+        
+        return result;
+    }
+
+}

Added: activemq/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/EndpointResolver/timer
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/EndpointResolver/timer?view=auto&rev=523078
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/EndpointResolver/timer
(added)
+++ activemq/camel/trunk/camel-core/src/main/resources/META-INF/services/org/apache/camel/EndpointResolver/timer
Tue Mar 27 14:03:50 2007
@@ -0,0 +1 @@
+class=org.apache.camel.component.pojo.timer.TimerEndpointResolver

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/pojo/timer/TimerRouteTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/pojo/timer/TimerRouteTest.java?view=auto&rev=523078
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/pojo/timer/TimerRouteTest.java
(added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/component/pojo/timer/TimerRouteTest.java
Tue Mar 27 14:03:50 2007
@@ -0,0 +1,64 @@
+/**
+ *
+ * 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.camel.component.pojo.timer;
+
+import java.util.concurrent.atomic.AtomicInteger;
+
+import junit.framework.TestCase;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.pojo.PojoComponent;
+import org.apache.camel.impl.DefaultCamelContext;
+
+/**
+ * @version $Revision: 520220 $
+ */
+public class TimerRouteTest extends TestCase {
+	
+    public void testPojoRoutes() throws Exception {
+
+        CamelContext container = new DefaultCamelContext();
+        
+        final AtomicInteger hitCount = new AtomicInteger();
+
+        PojoComponent component = new PojoComponent();
+        component.addService("bar", new Runnable(){
+			public void run() {
+		        System.out.println("hit");
+				hitCount.incrementAndGet();
+			}
+        });
+        container.addComponent("default", component);
+        
+        // lets add some routes
+        container.addRoutes(new RouteBuilder() {
+            public void configure() {
+                from("timer:foo?fixedRate=true&delay=0&period=500").to("pojo:default:bar");
+            }
+        });
+        
+        container.activateEndpoints();
+
+        // now lets wait for the timer to fire a few times.
+        Thread.sleep(1000*2);
+        assertTrue("", hitCount.get()>= 3 );
+        
+        container.deactivateEndpoints();
+    }
+}



Mime
View raw message