More working tests.
[castle.git] / AspectSharp / AspectSharp.Example / Application.cs
blob34dfce3e07f0a2ee92c4de1d93cf07263f99cb1c
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace AspectSharpExample
17 using System;
18 using System.Collections;
20 using AspectSharp;
21 using AspectSharp.Builder;
23 using AspectSharp.Example;
24 using AspectSharp.Example.Views;
25 using AspectSharp.Example.ContentProviders;
27 /// <summary>
28 /// Summary description for Application.
29 /// </summary>
30 public class Application
32 public static void Main()
34 // First we're going to execute two content providers and
35 // display the contents using the PlainTextView
36 SimpleExecution();
38 // Now we're going to execute adding a security
39 // mixin to content providers and security checking through
40 // interceptors
41 MixinAndInterceptorExecution();
43 // Altering the Hashtable
44 HashtableTest();
46 Console.WriteLine("\r\nPress any key to exit." );
47 Console.ReadLine();
50 private static void SimpleExecution()
52 Console.Out.WriteLine( " o0o First execution o0o " );
54 RequestPipeline pipeline = new RequestPipeline();
55 pipeline.Context["username"] = "Billy Paul Mckinsky";
56 pipeline.AddContentProvider( new StaticContentProvider() );
57 pipeline.AddContentProvider( new DynamicContentProvider() );
58 pipeline.View = new PlainTextView();
59 pipeline.ProcessRequest( Console.Out );
61 Console.Out.WriteLine();
64 private static void MixinAndInterceptorExecution()
66 Console.Out.WriteLine( " o0o Within security checking o0o " );
68 // For the sake of readability we're keep the aspect code here:
69 String contents =
70 " import AspectSharp.Example.Aop.Interceptors " +
71 " import AspectSharp.Example.Aop.Mixins " +
72 " " +
73 " aspect sample for [ AspectSharp.Example.ContentProviders ] " +
74 " include SecurityMixin " +
75 " " +
76 " pointcut method(* RetrieveContent())" +
77 " advice(SecurityCheckInterceptor)" +
78 " end" +
79 " " +
80 " end ";
82 AspectLanguageEngineBuilder builder = new AspectLanguageEngineBuilder( contents );
83 AspectEngine engine = builder.Build();
85 RequestPipeline pipeline = new RequestPipeline();
86 pipeline.Context["username"] = "Billy Paul Mckinsky";
87 pipeline.AddContentProvider( engine.WrapClass( typeof(StaticContentProvider) ) as IContentProvider );
88 pipeline.AddContentProvider( engine.WrapClass( typeof(DynamicContentProvider) ) as IContentProvider );
89 pipeline.View = new PlainTextView();
90 pipeline.ProcessRequest( Console.Out );
92 Console.Out.WriteLine();
95 private static void HashtableTest()
97 Console.Out.WriteLine( " o0o Changing default hashtable value o0o " );
99 // For the sake of readability we're keep the aspect code here:
100 String contents =
101 " import System.Collections " +
102 " " +
103 " aspect sample for Hashtable " +
104 " " +
105 " pointcut propertyread(* Item(*))" +
106 " advice(HashcodeDefaultValueInterceptor)" +
107 " end" +
108 " " +
109 " end ";
111 AspectLanguageEngineBuilder builder = new AspectLanguageEngineBuilder( contents );
112 AspectEngine engine = builder.Build();
114 IDictionary myHashTable = engine.WrapClass( typeof(Hashtable) ) as IDictionary;
116 String value = myHashTable["item"] as String;
117 Console.Out.WriteLine( "Default value is {0}", value );
119 Console.Out.WriteLine();