Return-Path: Delivered-To: apmail-camel-dev-archive@www.apache.org Received: (qmail 90641 invoked from network); 18 Dec 2009 12:05:04 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 18 Dec 2009 12:05:04 -0000 Received: (qmail 30683 invoked by uid 500); 18 Dec 2009 12:05:03 -0000 Delivered-To: apmail-camel-dev-archive@camel.apache.org Received: (qmail 30650 invoked by uid 500); 18 Dec 2009 12:05:03 -0000 Mailing-List: contact dev-help@camel.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@camel.apache.org Delivered-To: mailing list dev@camel.apache.org Received: (qmail 30640 invoked by uid 99); 18 Dec 2009 12:05:03 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 18 Dec 2009 12:05:03 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of willem.jiang@gmail.com designates 209.85.222.177 as permitted sender) Received: from [209.85.222.177] (HELO mail-pz0-f177.google.com) (209.85.222.177) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 18 Dec 2009 12:04:53 +0000 Received: by pzk7 with SMTP id 7so6060306pzk.30 for ; Fri, 18 Dec 2009 04:04:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=WO9Bh3hSsHHAeNM+g5/9VRvQRCW5gSRVWSuq9egKfck=; b=S3D6WBBubCUqlSWil0TpJTlsdPoDoti1l8Cjl1O8j5QSwmeI3PlLdSGqlakhUN6Zjy ACzfJ9bYPC8JAMsYOw0EHK24eR83JTenIgnAaiHH7f2JMXDsBkoGoZP2oETZjYrbHoHY t1fSAq4vuQceXCOctxXi/l+r03KZctBvL5dYg= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; b=TPs4rD6SUi7tzN3ERVji+35KtrVpsaF0ZJXXO58pmN/PQyNt5HJaAkl5ZE76yTNaYI AdcNEbYENvWT6/uxWNSPFvr+qjkeZJr1+RBwWX8Ps4s3GJyK81gKR2KD1Er/8kHy76on Z/NntqrQkg6jY9dwH3o+5p0SscMTQxMbZHMNQ= Received: by 10.141.101.15 with SMTP id d15mr2644451rvm.284.1261137871659; Fri, 18 Dec 2009 04:04:31 -0800 (PST) Received: from ?192.168.0.158? ([125.33.124.155]) by mx.google.com with ESMTPS id 22sm2330300pzk.10.2009.12.18.04.04.27 (version=TLSv1/SSLv3 cipher=RC4-MD5); Fri, 18 Dec 2009 04:04:31 -0800 (PST) Message-ID: <4B2B6FC6.3030708@gmail.com> Date: Fri, 18 Dec 2009 20:04:22 +0800 From: Willem Jiang User-Agent: Thunderbird 2.0.0.23 (Macintosh/20090812) MIME-Version: 1.0 To: dev@camel.apache.org Subject: Re: svn commit: r892199 - /camel/trunk/components/camel-protobuf/src/main/java/org/apache/camel/dataformat/protobuf/ProtobufDataFormat.java References: <20091218095430.A1FBE23889D2@eris.apache.org> <5380c69c0912180220q3502bdc1j33b2848b9663aa5@mail.gmail.com> <5380c69c0912180221h13a99f88tf30fb44d47934b0c@mail.gmail.com> In-Reply-To: <5380c69c0912180221h13a99f88tf30fb44d47934b0c@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org Hi Claus, Thanks for point issue out. I added a lock on loading the class, it should be better now ;) Willem Claus Ibsen wrote: > On Fri, Dec 18, 2009 at 11:20 AM, Claus Ibsen wrote: >> Hi >> >> Actually the AtomicBoolean cannot safely be used for initializing code blocks? >> >> You can still have concurrent threads invoking the boolean and one >> thread will see it as false and the other as true. >> What it helps is that there are only one ever that sees it as false >> and therefore only one thread that will initialize it. >> >> But in the mean time another thread could have seen it as true, but >> its still not initialized because the first thread is currently doing >> that. >> e.g. there are no locks. >> >> You have to use the synchronized or a Lock object. >> >> See for example camel-jaxb and how it use the synchronized. >> >> Its not bad to use as synchronized is heavily optimized in JDK6. >> >> >> A better idea would be to implements Service (extends ServiceSupport) >> and do initialization in the start method. >> I am pretty sure Camel will invoke these start/stop on data formats as >> well. Could you try that? If not we should get that done. >> Then all kind of initialization can be done using the Service interface. >> > > Ah yeah you need to do it at runtime sine you need access to CamelContext. > I wonder if implementing CamelContextAware is sufficient? > > >> >> >> On Fri, Dec 18, 2009 at 10:54 AM, wrote: >>> Author: ningjiang >>> Date: Fri Dec 18 09:54:30 2009 >>> New Revision: 892199 >>> >>> URL: http://svn.apache.org/viewvc?rev=892199&view=rev >>> Log: >>> CAMEL-2148 using the ClassResolver from the CamelContext to load class >>> >>> Modified: >>> camel/trunk/components/camel-protobuf/src/main/java/org/apache/camel/dataformat/protobuf/ProtobufDataFormat.java >>> >>> Modified: camel/trunk/components/camel-protobuf/src/main/java/org/apache/camel/dataformat/protobuf/ProtobufDataFormat.java >>> URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-protobuf/src/main/java/org/apache/camel/dataformat/protobuf/ProtobufDataFormat.java?rev=892199&r1=892198&r2=892199&view=diff >>> ============================================================================== >>> --- camel/trunk/components/camel-protobuf/src/main/java/org/apache/camel/dataformat/protobuf/ProtobufDataFormat.java (original) >>> +++ camel/trunk/components/camel-protobuf/src/main/java/org/apache/camel/dataformat/protobuf/ProtobufDataFormat.java Fri Dec 18 09:54:30 2009 >>> @@ -20,10 +20,12 @@ >>> import java.io.InputStream; >>> import java.io.OutputStream; >>> import java.lang.reflect.Method; >>> +import java.util.concurrent.atomic.AtomicBoolean; >>> >>> import com.google.protobuf.Message; >>> import com.google.protobuf.Message.Builder; >>> >>> +import org.apache.camel.CamelContext; >>> import org.apache.camel.CamelException; >>> import org.apache.camel.Exchange; >>> import org.apache.camel.InvalidPayloadException; >>> @@ -34,6 +36,8 @@ >>> public class ProtobufDataFormat implements DataFormat { >>> >>> private Message defaultInstance; >>> + private String instanceClassName; >>> + private AtomicBoolean setDefaultInstanceHasBeenCalled = new AtomicBoolean(false); >>> >>> /** >>> * @param defaultInstance >>> @@ -51,11 +55,15 @@ >>> >>> public void setInstanceClass(String className) throws Exception { >>> ObjectHelper.notNull(className, "ProtobufDataFormat instaceClass"); >>> - Class instanceClass = ObjectHelper.loadClass(className); >>> + instanceClassName = className; >>> + } >>> + >>> + protected Message loadDefaultInstance(String className, CamelContext context) throws CamelException, ClassNotFoundException { >>> + Class instanceClass = context.getClassResolver().resolveMandatoryClass(className); >>> if (Message.class.isAssignableFrom(instanceClass)) { >>> try { >>> Method method = instanceClass.getMethod("getDefaultInstance", new Class[0]); >>> - defaultInstance = (Message) method.invoke(null, new Object[0]); >>> + return (Message) method.invoke(null, new Object[0]); >>> } catch (Exception ex) { >>> throw new CamelException("Can't set the defaultInstance of ProtobufferDataFormat with " >>> + className + ", caused by " + ex); >>> @@ -64,7 +72,6 @@ >>> throw new CamelException("Can't set the defaultInstance of ProtobufferDataFormat with " >>> + className + ", as the class is not a subClass of com.google.protobuf.Message"); >>> } >>> - >>> } >>> >>> /* >>> @@ -82,8 +89,15 @@ >>> * java.io.InputStream) >>> */ >>> public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception { >>> + >>> if (this.defaultInstance == null) { >>> - throw new CamelException("There is not defaultInstance for protobuf unmarshaling"); >>> + if (instanceClassName == null) { >>> + throw new CamelException("There is not defaultInstance for protobuf unmarshaling"); >>> + } else { >>> + if (!setDefaultInstanceHasBeenCalled.getAndSet(true)) { >>> + defaultInstance = loadDefaultInstance(instanceClassName, exchange.getContext()); >>> + } >>> + } >>> } >>> Builder builder = this.defaultInstance.newBuilderForType().mergeFrom(inputStream); >>> if (!builder.isInitialized()) { >>> >>> >>> >> >> >> -- >> Claus Ibsen >> Apache Camel Committer >> >> Author of Camel in Action: http://www.manning.com/ibsen/ >> Open Source Integration: http://fusesource.com >> Blog: http://davsclaus.blogspot.com/ >> Twitter: http://twitter.com/davsclaus >> > > >