■Introduction to ColdSpring AOP Aspect Oriented Programming is a fairly new programming model that assists in handling far reaching services, like logging, security, transactions and object caching, in your model. Services like these are beginning to be referred to as ‘crosscutting concerns’, because they are applied broadly across your application. In order to add a service such as logging to an application, you typically find yourself applying very similar code all throughout your model, which when scrutinized, probably has nothing to do with the actual functionality of your components. AOP allows you to insulate these types of crosscutting concerns from the rest of you application, and apply them broadly based on certain policies that you as the developer define. アスペクト指向プログラミングは、modelのなかで、ロギング、セキュリティ、トランザクション及びオブジェクトキャシングなどの遠くに位置するサービスの処理をアシストするための全く新しいプログラミングモデルです。 これらのサービスはアプリケーションを通じて広く利用されるので、"横断的関心事:crosscutting concerns"として参照され始めています。 あるアプリケーションにロギングのようなサービスを加えるためにアプリケーションをよく吟味すると、おそらくコンポーネントの実際の機能性とは全く関係ない非常に似たようなコードがモデル中に散見されます。 AOP は、アプリケーションから横断的関心事となるAOPパターンを隔離できるので、開発者の定義するポリシーに広く基づいて応用できます。 Using AOP is basically a process of looking at the types of concerns in your application and then breaking them apart into primary concerns, like your managing your shopping cart or navigating through and administering your product catalog, your application’s primary business logic, and crosscutting, generalized concerns, such as logging and security. These crosscutting concerns become what are known as ‘aspects’, and with AOP can be written singularly focused on the issues they solve, becoming more portable and reusable. These aspects are then applied to your model according to policies that you define, through a process known as ‘weaving’, which is performed by the AOP framework. AOP を使用することは、アプリケーション中の関心事のパターンを探し出し、ショッピングカートのマネージメントやナビゲーション及び商品カタログの管理など、主要な関心事をアプリケーション主要ビジネスロジックと、ロギングやセキュリティのような汎用的な関心事にバラバラに分割する基本的なプロセスを行うことです。 これらの横断的な関心事は、アスペクト(局面)として知れれるものになります。そして、AOPを用いると、よりポータブルで再利用性に優れた解決すべき核心だけにフォーカスされたプログラムを書くことができます。 これらのアスペクトは、'weaving'と呼ばれるプロセスを通じ、AOPフレームワークによって実行される定義済みのポリシーに従って、modelに適用されます。 Defining policies is a matter of identifying ‘join points’ in your model, specific areas where you would like to apply the ‘advice’ of your aspects, meaning the specific action that you would like to take place at a certain time in your application. Join points are created using ‘pointcuts’ which are defined with wildcards or regular expressions to identify methods in your objects. Don’t get too worried about the terminology here; the primary concept is that you already have objects with methods in them that you want to add functionality to. You do this through advice. There are three types of advice available in ColdSpring AOP, before, after, and around advice. Before advice is run before the join point will be performed; after advice will be run after the join point. Around advice is like a combination of both. Around advice is actually quite powerful because it is given full control of the execution of the join point and it has the ability to advise errors through try/catch blocks. ポリシーを定義することは、modelコンポーネント内で、アスペクトの'advice'(アプリケーション内のあるときに起こしたい特定のアクションを意味します。)を適用したい特定の領域となるジョインポイント:joinpointをおよそ同定することです。 ジョインポイントは、オブジェクト内のメソッドを同定するためにワイルドカードや正規表現によって定義されたポイントカット:pointcutを使って生成されます。 ここでは、専門用語にあまり惑わされないでください。一番大事なコンセプトは、機能性を追加したいオブジェクト内にメソッドを有するオブジェクトを既に持っているということです。 ColdSpring AOPには、利用可能な3つの型式のアドバイス('before'、'after'、そして'around')があります。 'Before'アドバイスは、ジョインポイントが実行される前にコールされます。 'After'アドバイスは、ジョインポイントの実行後にコールされます。 'Around'アドバイスは、両方のコンビネーションです。'Around'アドバイスは、かなり強力です。なぜなら、ジョインポイントの実行の完全な制御を与えられ、try/catchブロックを通してエラーをアドバイスできるからです。 The final concepts to understand in AOP are ‘target’ and ‘proxy’ objects. The target object is object in your model that you want to apply your aspects to. Through the process of weaving, the AOP framework will use the policies you defined via pointcuts to build a new proxy object containing the configured advice, which, and here’s the most important part, appears to be one in the same as the target object to the rest of your application. Now I know a lot of this is sounding confusing and a bit difficult, but it’s important to understand that most of the work is actually done for you. You will primary concern yourself with creating the advice objects themselves. Pointcuts are defined through configuration and ColdSpring AOP handles the rest. In the context of ColdSpring, the configuration is done through an xml file, and the proxy object can be swapped out for the target object with minimal effort. AOP で理解するべき最終的な概念は、'ターゲット:target' 及び'プロキシー:proxy'オブジェクトです。 ターゲットオブジェクトは、アスペクトを適用したいmodel内のオブジェクトです。 'weaving'プロセスを通して、AOP フレームワークは、設定済のアドバイスを含む新規のプロキシーオブジェクト(ここに最も重要な部分があります。 アプリケーションの残りに対するターゲットオブジェクトして唯一無比となるプロキシー)を生成するようにポイントカットで定義されたポリシーを使用します。 多くの仕事のほとんどが実際に終ったことを理解することは重要ですが、混乱と少しの困難さがあることもたくさん分かっています。アドバイスオブジェトをそれ自身に作成することによって、主要な関心をそれ自身に向けることができます。 ポイントカット はコンフィグレーションファイルを通して定義され、ColdSpring AOP はその残りを扱います。 ColdSpring のコンテキストでは、コンフィグレーションはxml ファイルを通して実行され、ターゲットオブジェクトは最小の努力でプロキシーオブジェクトを通して取り次がれます。 I’ll post some actual examples after I get a chance to run through them at the conference and get some feedback. Hopefully I’ll get a chance to meet a few people who just may have read some of this there! 私はあるコンファレンスでデモし、フィードバックを得るチャンスを得られたらいくつかのサンプルを投稿する予定です。うまく行けば、そのコンファレンスでこの記事を読んでくれた数人のメンバーと会うチャンスがあるでしょう! posted by Chris Scott at 5:33 PM ■ColdSpring AOP Tutorial - Part One Since I have already given an overview of ColdSpring’s AOP concepts, I’m going to just dive right into things. For part one of the developing with ColdSpring AOP, I’ll show how you can use a simple form of Advice called Before Advice to provide logging services to a model component. These examples are from the Klondike Records application that I showed at the frameworks conference last week, which we will also be releasing as an example application. I’ll show you how to define an Advice cfc, which will extends coldspring.aop.BeforeAdvice, how to define Pointcuts via Advisors in a standard ColdSpring config file, and how to use coldspring.aop. ColdSpring AOP のコンセプト概要は既に説明したので、プログラミング詳細に没頭してみましょう。 ColdSpring AOPによるアプリケーション開発の第一弾として、modelコンポーネントにロギングサービス機能を提供するBeforeアドバイスと呼ばれるアドバイスの簡単なパターンを示します。 これらの例は、先週のフレームワークコンファレンスでデモした近い将来アプリケーション例としてリリースする予定のKlondike Recordsアプリケーションのパターンです。 coldSpring.aop.BeforeAdviceを継承したAdvice.cfc、ColdSpringコンフィグレーションファイルにおけるポイントカットとアドバイザーの定義方法、そして、coldspring.aopコンポーネント群の使用方法を示します。 ProxyFactoryBean to wrap the Advice around methods of a target object by creating a Proxy object, containing the target. Think about the proxy object like an egg. The object you want to apply logging to would be the yoke and the Advice that provides the logging functionality is the egg white surrounding it. When the proxy factory creates this egg, and this is the great part, everything in you application thinks that it’s the same object that’s in the yoke! OK, so so let’s get right into it. First we’ll define a model component, which will be called CatalogDAO.cfc which we’ll then add logging services to. ターゲットオブジェクトを含むあるプロキシープロジェクトを作成することによって、あるターゲットオブジェクト内メソッド周辺のAdviceをラップすることがProxyFactoryBeanによって可能です。 プロキシーオブジェクトは卵のようなもとだと考えてください。 ロギングさせたいと望むオブジェクトは卵の黄身であり、ロギング機能を提供するアドバイスは、それを囲む卵白だということです。 プロキシーファクトリーがこの卵を生成するとき、そうこれが非常に重要なことですが、黄身のなかにあるオブジェクトと同じオブジェクトだとアプリケーションは考え、「はいはい、その中に入っていきましょう。」といいます。 最初にロギングを追加するCatalogDAO.cfc と呼ばれるmodelコンポーネントを定義してみましょう。 SELECT QUERY … INSERT QUERY … UPDATE QUERY … Obviously we’re skipping some details, but the point is that this is just a standard model component; we don’t extend anything or put any special code here. The component should be completely usable with or without AOP. In my example app you will see that this dao is used as part of a service component, which is used by the catalogListener in the mach-ii app. The nice thing about using AOP to provide logging is that neither the dao nor the service component that uses it will have any code in them pertaining to logging. They are only concentrated on fetching and storing records, cohesion. I like to use log4j, so I’ll create a LoggingService cfc to handle the configuration details and provide a common api to my Aspects. Also, if I want to switch this out to use cflog or another implementation, I can do that in one place. So here’s the LoggingService cfc, but I’ll skip showing how to actually configure log4j, since I have already shown that in an earlier post. This service basically just retrieves the logger and wraps its info() method. 明らかに、細部はとばしていますが、これがちょうど標準的なModelコンポーネント(何も継承していないし、また特別なコードもない)であることがポイントです。 コンポーネントは、AOP の有無にかかわらず、完全に再利用可能であるべきです。 このサンプルアプリケーションでは、Mach-IIアプリケーションのcatalogリスナーによって利用されているサービスコンポーネントの一部としてdaoが使用されているということです。 ロギング提供のためにAOP を使用することの素晴らしいことは、ロギングに関係するコードを、daoやサービスコンポーネントの中に一切書く必要がないということです。 daoやサービスコンポーネントは、レコードデータのフェッチやストア及び結合に専念するだけです。 私はlog4j をよく使用するので、コンフィグレーションの細部取扱いと共通APIをアスペクトに提供するために、LoggingService.cfcを作成しています。 また、cflog を使用するか別の実装に転換したいと思うので、1 つの場所のロギングサービスを利用することができるようにしています。 LoggingService.cfc を以下に示しますが、以前の投稿で既にそれを示してしまったので、log4jの実際のコンフィグ方法は今回省略します。 このサービスは、基本的にロガーからログ情報を取り出し、info() メソッドに取次ぎます。 Ok, now onto the AOP part. I will first create a Before Advice to retrieve information about the current method being invoked and send it off as the message argument to the logging service’s info() method. Here’s the loggingBeforeAdvice cfc. それでは、AOPパートに移りましょう。 最初にBeforeアドバイスを作成し、呼び出し先のカレントメソッドの情報を取り出し、ロギングサービスのinfo()メソッドに引数messageとして送り出します。 loggingBeforeAdvice.cfcを以下に示します。 * klondike loggingAdvisor That’s it for the configuration. When you ask the ColdSpring bean factory for your catalogService, ColdSpring will create the ProxyFactoryBean and ask it to return a proxy object. The ProxyFactoryBean will in turn get a reference to the target object, the real DAO object, get the Advisor which contains the Aspect we created, causing the loggingService to be created, and will finally generate an entirely new cfc, which contains the target object, the proxy for the catalogDAO. This proxy object will be the same type as the target object and will contain all the same methods as the target, except that it will now flow every method through an instance of the loggingBeforeAdvice. If I want to add the same advice say to a gatewayObject, all I have to do is create another target object, and another ProxyFactoryBean and give it the same Advisor. If this doesn’t seem powerful enough for you, you can also create a new Advice, say a securityAdvice, and create a new Advisor, matching only the ‘save’ method, and give this to the ProxyBeanFactory we defined above. That would make that bean definition look something like this: それでは、コンフィギュレーションに移りましょう。 catalogService をColdSpringのbeanファクトリーに要求すると、ColdSpring はProxyFactoryBean を作成し、ProxyFactoryBeanを生成し、プロキシーオブジェクトを返すようにそれに依頼します。 ProxyFactoryBeanは、次に実際のDAOオブジェクトとなるターゲットオブジェクトへの参照を得て、生成したアスペクトを含むAdvisorを得ます。このAdvisorは、loggingServiceを生成することになります。 また、ProxyFactoryBeanは、最終的にcatalogDAOへのプロキシー機能を有する全く新規のcfcを作り出します。このプロキシーオブジェクトは、ターゲットオブジェクトと全く同一のタイプで、loggingBeforeAdviceのインスタンスとして、メソッドの実行毎に追跡することを除いて、ターゲットと全く同一のメソッドを含みます。 あるGatewayオブジェクトに同じAdviceをしゃべるように追加する場合でも、他のターゲットオブジェクトの生成及び他のProxyFactoryBeanの生成とAdvisorへのくくりつけは、全く不要です。 もし、不十分であるようなら、新規のAdvice、即ちsecurityAdviceを作成し、また'save'だけにマッチする新規Advisorを作成し、前述のProxyFactoryBeanに与えることができます。 以上のことをbean定義にすると、以下のようになります: loggingAdvisor securityAdvisor Adding the securityAdvisor after the loggingAdvisor causes it to be chained after the loggingAdvice, so if that Advice does something like throw an ‘InvalidCredentials’ error, the loggingAdvice will get a chance to write to the log before the method execution stops. So that’s just a small piece of the framework, the least powerful type of Advice we have available. After Advice is very similar to Before Advice, except that it occurs after method execution, giving you access to the return variable from the method as well as argument collection that was passed into the method. Wow, so that’s a pretty big blog post! I hope I’ve wet your appetite, and stay tuned because a super-prealpha-ubermethodology-oops-sorry-release is just around the corner! loggingAdvisorに続いてsecurityAdvisorを追加すると、loggingAdviceに続いてsecurityAdviceをチェーンさせることになるので、loggingAdviceが何らかの'InvalidCredentials'エラーを投げるような何かを行った場合でも、loggingAdviceはメソッドの実行停止前にログを書き込むことができます。フレームワークのほんの小さな一部分ですが、Adviceは十分大きな力を発揮します。 AfterAdviceは、メソッドの実行後にインターセプトを発生させる点を除いて、BeforeAdviceに大変似ています。 これによって、メソッドに渡された引数コレクションなどをメソッド戻り値としてアクセスできるようになります。 さあて、かなり大きいblog の記事になってしまいましたね。 お気に召していただけましたか?あるsuper-prealpha-ubermethodology-oops-sorry-releaseの完成間近なので、このへんで失礼します。 posted by Chris Scott at 8:15 PM ■ColdSpring, Model-Glue and Logging with AOP After a little bit of trial and error, I just succeeded in (a) integrating ColdSpring with Model-Glue, (b) defining logging advice after a method call and (c) actual logging results returned from methods! ほんの少しのトライアルとエラーを経て、 (a)ColdSpringとModel-Glueを統合 (b)メソッドコール後のロギング用Adviceの定義 (c)メソッド実行後のロギング結果取得 に成功した。 Dave Ross shows how to use the Model-Glue adapter in ColdSpring - the returnType= attribute should actually specify the fully-qualified return type tho'. That combined with Chris Scott's AOP tutorial and a quick read of the source code (download 0.2.1 from cfopen). Dave Rossは、ColdSpringにおけるModel-Glueアダプターの使用方法を示すなかで、returnTypeの属性指定を完全記述すべきであるとしている。Chris ScottのAOPチュートリアルとソースコードのクイックリードは結合されている(cfopenからVer0.2.1をダウンロードした) My LoggingAfter CFC extends coldspring.aop.AfterReturningAdvice, implements afterReturning() which logs a message and then returns arguments.returnVal (very important!). 私のLoggingAfter.CFCは、coldspring.aop.AfterReturningAdviceを継承し、メッセージをロギングし、arguments.returnValを戻すafterReturning()を実装している。(とても重要) My ColdSpring XML file specifies: 私のColdSpringXMLファイルの定義: * xxxTarget - my actual bean that I want to log methods in * xxx - a coldspring.aop.framework.ProxyFactoryBean that references xxxTarget and uses the logAfterAdvisor intercepter (below) * logAfterAdvice - my simple logging class * logAfterAdvisor - a coldspring.aop.support.NamedMethodPointcutAdvisor that specifies my logAfterAdvice as the advice property and * as the mappedNames (matches anything) * xxxTarget - ログを取りたいメソッドのターゲットbean名 * xxx - xxxTargetを参照し、logAfterAdvisorインターセプター(以下)を使用するcoldspring.aop.framework.ProxyFactoryBean * logAfterAdvice - 単一のロギング機能を実装したのクラス * logAfterAdvisor - logAfterAdviceをAdviceのpropertyとして、* をmappedNamesとして指定した coldspring.aop.support.NamedMethodPointcutAdvisor The results of calling methods in xxxTarget are logged automatically! xxxTargetに指定されたメソッドの呼び出し結果が自動的にロギングされます! Logging can be disabled by renaming the xxx and xxxTarget beans (so that my application code calls the actual bean, via the name xxx, instead of the proxy). xxx BeanとxxxTarget Beanをリネームすることで、ロギング不可になります。(proxyの代わって、xxxによって実際のBeanを呼び出すので) Very slick... 非常に滑らかです・・・ Update: see the next blog entry for an even simpler integration! Posted At : 3:21 AM. | Posted By : seancorfield | modelglue, coldspring, cf | Comments (5) ■Easy ColdSpring in Model-Glue Dave Ross and Chris Scott are doing an _amazing_ job with the ColdSpring project. For those of you who aren't familiar with Spring, it's a Java framework that provides a number of services such as IoC (like ConfigBeans in Model-Glue), Aspect Oriented Programming, and ORM functionality amongst many things. Right now, ColdSpring is at 0.2.1 and provides IoC and Aspect Oriented functionality. Dave RossとChris Scottが、ColdSpring のプロジェクトで_ 驚かせる_ 仕事をしている。Springをよく知られていない方々のために紹介すると、IoC (Model-GlueのConfigBeansのようなもの)、アスペクト指向プログラミング、そして多くのなかにあるORM(オブジェクトロールモデル)機能。今現在、ColdSpring は0.2.1 にあり、IoC 及びアスペクト指向機能を提供する。 Summary for the impatient - how to use ColdSpring seamlessly Model-Glue in four steps: 気短かな方のための概要- 4 つのステップでColdSpring とModel-Glueを使用する方法: 1. Get latest from the BER Subversion repo. 2. Add to your ModelGlue.xml 3. Change the beanMappings setting to the path to your ColdSpring xml: 4. Instead of GetModelGlue().GetConfigBean("myBeanFileName.xml"), use GetModelGlue.GetConfigBean("ColdSpringBeanId") 1. Model-GlueのBERサブバージョンレポジトリから最新バージョンを入手。 2. ModelGlue.xmlに以下を追加。 3. ColdSpring xmlのパスをセットしているbeanMappingsを変更。 4. GetModelGlue().GetConfigBean("myBeanFileName.xml")の代わりに、 GetModelGlue.GetConfigBean("ColdSpringBeanId")を使用。 ColdSpring is very nice, and Dave's done some nice work on Model-Glue integration that I decided to wrap into the framework itself, building some functionality into the Bleeding Edge Release of Model-Glue (svn://clearsoftware.net/clearsoftware.net/ModelGlue) that'll let you seamlessly use ColdSpring. ColdSpringは、とてもすばらしい。そして、Daveは私がフレームワークに取り入れようとしていたModel-Glueとの統合をModel-GlueのBERリリース版にいくつかの機能を追加することで、見事にやってくれた(svn://clearsoftware.net/clearsoftware.net/ModelGlue) 。これで、Model-GlueからColdSpringをシームレスに使えるようになった。 Just add the following line of code to your ModelGlue.xml file: 使用方法は、ModelGlue.xmlに以下の1行を追加するだけです。 Then change the beanMappings setting to your ColdSpring .xml file: そして、ColdSpring .xmlのbeanMapping設定を変更します。 Once that's done, if you do getModelGlue().getConfigBean(), instead of passing the filename of an XML file to get, just pass the ID of the ColdSpring bean to load. 一度、前述のことを行うと、XMLファイルを指定することなしに、getModelGlue().getConfigBean()を行うと、ColdSpringBeanのIDをローダーに渡してくれます。 If you get the latest M-G BER, you'll notice there's a modelgluesamples/coldspring app that shows it in action. The thing just _worked_ on the first try. 最新のModelGlue-BER版を入手すると、実際にそれを示したmodelgluesamples/coldspringサンプルアプリケーションがあることに気づくでしょう。まず、最初にそれを動かしてみることです。 Credit goes to Dave - he basically wrote the code to do it all, I just used the pluggable part of Model-Glue 1.0 to make it go. Daveからの情報によると、彼は基本的なところはすべて書き上げており、私はその一部をModel-Glue Ver1.0に プラグイン的に使いました。 Note: There are things to think about when using ColdSpring as the native bean factory in Model-Glue. You won't be able to use existing ChiliBean beans unless you create the ChiliBean BeanFactory by hand, so it's not something to implement mid-project. However, it wouldn't be hard to move ChiliBean XML files into a ColdSpring XML doc. 注意: ColdSpringをModel-Glueのネイティブなbeanファクトリーとして使用するにはまだまだ考慮すべきことがたくさんあります。ChiliBeanによるBeanファクトリーを手動で作成しないことには、既存のChiliBean Beanを使えなくなるでしょう。しかしながら、ChiliBeanXMLの内容をColdSpringXMLに移すことは、そう難しくはないでしょう。 Posted At : 10:12 AM. | Posted By : joe | Model-Glue Framework, OOP ColdFusion | Comments (2) ■ColdSpring / Model-Glue Autowiring I mentioned this feature earlier as an add-on but wanted to let folks know that the next release of ColdSpring will include the Model-Glue autowiring code (Dave Ross gave me commit access to the CVS repository - thanx!). 以前の記事でアドオンとして、ColdSpring / Model-Glue Autowiringを紹介しましたが、ColdSpringの次期バージョンにModel-Glue autowiringが実装されることになりました。(Dave RossがCVSレポジトリにアクセスすることの許可をくれました、サンクス Dave!) That means that the Model-Glue / ColdSpring integration will be as simple as: このため、Model-Glue と ColdSpring の統合は非常にシンプルになりました。 Specify the ColdSpring bean factory loader and location of the ColdSpring bean definition file in ModelGlue.xml: ModelGlue.xmlのなかに、ColdSpring bean factory loaderとColdSpring bean定義ファイルのロケーションをを指定します。 Adding the autorwiring controller (as the first controller): そして、第一のcontrollerとして、autorwiring controllerを追加します。 ...other controllers... Adding setXxx(yyy) methods to your controllers, where xxx is the name of the ColdSpring bean you want to be injected automatically. 各controllerにsetXxx(yyy) メソッドを追加します。xxxは、自動的に投射したいColdSpring bean名です。 Posted At : 2:25 PM. | Posted By : seancorfield | modelglue, coldspring, cf | Comments (2)