CSC/ECE 517 Fall 2009/wiki2 17 va: Difference between revisions
(Grammar revisions) |
|||
(One intermediate revision by the same user not shown) | |||
Line 86: | Line 86: | ||
$object->$method(); | $object->$method(); | ||
In the example with reflection, $object and $method can be | In the example with reflection, $object and $method can be viewed by the program and even changed at run time. This allows the program to change its own behavior. This is an example of reflection because the program can see itself and change itself. | ||
:*'''''Example''': Reflection in Ruby [http://en.wikipedia.org/wiki/Reflection_(computer_science) 5] | :*'''''Example''': Reflection in Ruby [http://en.wikipedia.org/wiki/Reflection_(computer_science) 5] | ||
Line 106: | Line 106: | ||
=='''How metaprogramming and reflection enhance SOA'''== | =='''How metaprogramming and reflection enhance SOA'''== | ||
Service-oriented Architecture is enhanced when the underlying programs are able to dynamically adapt to the data received. Metaprogramming and Reflection make this possible. | Service-oriented Architecture is enhanced when the underlying programs are able to dynamically adapt to the data received. Metaprogramming and Reflection make this possible. To see how this is so, it will be helpful to look at a few potential uses. | ||
The services | One such use would be a service that writes code based on the inputs. The services within SOA make use of the data passed to them. When the passed data represents code, for example 'print "Hello World"' in a language independent way, the receiving service could use metaprogramming to write code to just that. The service would then use reflection to determine what methods are available, in our case 'print', and would perform that task. If the service logic was to "repeat 3 times", then the code would ultimately print "Hello World" that number of times. XML may be used to transfer this message as follows: | ||
XML may be used to transfer | |||
:*'''''Example''' XML Data: | :*'''''Example''' XML Data: | ||
Line 119: | Line 117: | ||
</message> | </message> | ||
A more practical use of reflection and metaprogramming in a SOA would be a service that | A more practical use of reflection and metaprogramming in a SOA would be a service that checks code for security holes. The code could be sent to the service via whatever protocol is chosen. This service could use reflection to examine the code. Next, the service would evaluate the code for common security flaws. If the program is determined to be safe enough, the service may run the code in a safe environment and perform further testing. The service would output the results of the analysis, and perhaps even output an auto-corrected program upon completion. | ||
Compilers are a good current illustration of metaprogramming and reflection working in a service. The compiler may be viewed as a service within a SOA. It can be run on many different systems, which is a major benefit of SOA's. Data (the code) is sent to a service (the compiler) which then translates it to machine code for the particular system. Metaprogramming is at work here, because each line of code is generally used to write one or more lines of machine code (code directly executable by the computer). The code and compiler effectively write a new program in machine code. Reflection may take place when symbols generated by one section of code are referenced by another section of code. [http://dvanderboom.wordpress.com/2008/11/06/programming-language-directions/ 11] | |||
A pattern emerges when studying these examples. Metaprogramming and reflection | A pattern emerges when studying these examples. Metaprogramming and reflection are generally used within the service to enhance the capability of the service. The data passed to the service is often what guides how metaprogramming and reflection take place. | ||
=='''Conclusion'''== | =='''Conclusion'''== |
Latest revision as of 01:27, 14 October 2009
Service Oriented Architecture 3 (or SOA) describes how to design a software system and connect the separate components using services. Reflection and metaprogramming are two powerful concepts that directly support the principles of SOA. This article covers the following goals:
- Provides a simple understanding of reflection, metaprogramming, and SOA's.
- Explains how these concepts are interrelated, specifically how reflection and metaprogramming support SOA's.
Service Oriented Architecture (SOA), Reflection, and Metaprogramming
An overview of the concepts
Service Oriented Architecture
Service Oriented Architecture (or SOA) is a concept in computing that defines the interaction of different software in terms of protocols and functionality. A SOA can be viewed as containing multiple services which may be linked as desired by protocols. The SOA is designed so that each functional unit of software in a system is isolated from each other and provides a service without directly making subroutine calls to each other. This is generally accomplished by passing messages. 3
SOA offers the following technical advantages:
- Offers services across platforms
- Allows services to be split across systems and networks
- Allows services to be location independent and close to their business units
- Provides a bridge between incompatible technologies
- Leverages existing technologies
- Reduces dependency on custom systems
The information being used to communicate between services must contain data, sufficient detail about the characteristic of the data and must remain independent of the underlying platform and programming language. Although SOA does not specify the format to transfer data, programmers have generally used XML for this purpose. Furthermore, Metadata (data that describes the actual data) 6 in the SOA must: 3
- Be easy to configure
- Provide an easy way to discover services
- Provide an easy way to incorporate services
- Remain coherent
- Preserve data integrity
- Be easy to manage
A simple example of metadata is a README file that specifies the inputs and outputs to a program. The inputs and outputs of the program would be the data, and the README file is metadata that describes this data.
SOA does not limit the protocol used to transfer the data. A wide variety of technologies can be used including SOAP, REST and RPC. This is generally left for the programmer of the system.
We created this simplified image to illustrate the basic principles of service oriented architectures.
- Services may be used by multiple applications.
- Services do not call on each other.
- Services communicate via protocols.
- An example protocol is provided, in which the application passes x to the Sin function, and Sin(x) is returned.
- Because of the protocol link, services may be written in different programming languages, or even be hosted on different computers with different operating systems, which allows for great flexibility.
Metaprogramming
Metaprogramming is the writing of computer programs that write other computer programs. This allows the programmer to complete tasks faster than if (s)he had to code everything manually. A compiler is an excellent example of a metaprogram. It allows the programmer to code in a higher level language and then the compiler will convert that program into machine language. 7
- Example: Metaprogramming in Ruby [2]
class Person attr_accessor :name end
In the example above, attr_accessor is used to create 'setters' and 'getters' for the name attribute via metaprogramming. From this statement, code is automatically generated that provides a function to write the name (setter) and a function to read the name (getter).
- Example: Metaprogramming in Ruby 9
Array.send(:define_method, :ducky) { puts 'ducky' }
This example does something particularly interesting. At run time, it defines a new method called ducky that prints out the word "ducky". This line modifies the program at run time!
Another example in Ruby is the ActiveRecord library. It dynamically creates accessor methods that map directly to each column in the database.
- Example: ActiveRecord metaprogramming in Ruby [10]
class Movie < ActiveRecord::Base end
movie = Movie.create movie.title = "Doctor Strangelove" movie.title # => "Doctor Strangelove"
In the example above, we create a new instance of Movie, which subclasses ActiveRecord. We then directly access the database's title column using the setter method 'title=' and then the getter method 'title'. These methods are not defined anywhere in the source code, but instead, ActiveRecord automatically defines them based on the database column.
Reflection
Reflection is a specific type of meta-programming and emphasizes dynamic program modification. It is the ability of a programming language to inspect its own code and can therefore be used to extend the language beyond its usual capabilities. For example, it is possible to use reflection to inspect an object for a desired function and execute it dynamically.
- Example: Reflection in Perl 5
# without reflection my $foo = Foo->new(); $foo->hello(); # with reflection my $class = "Foo"; my $method = "hello"; my $object = $class->new(); $object->$method();
In the example with reflection, $object and $method can be viewed by the program and even changed at run time. This allows the program to change its own behavior. This is an example of reflection because the program can see itself and change itself.
- Example: Reflection in Ruby 5
# without reflection Foo.new.hello # with reflection Object.const_get(:Foo).new.send(:hello)
In this example, the first and second lines illustrate how the code is written without reflection. The first line is a comment, and the second line is the actual code. The code calls on Foo.new.hello, which calls on the object Foo, method new, and from what is returned on that, calls the method hello. The second section does practically the same thing, but with reflection. The main difference is that the object called by :hello can be determined at run time.
- Example: More Reflection in Ruby 8
ObjectSpace.each_object(Numeric) {|x| p x }
In this example, Ruby can determine all the objects that are defined. This particular line of code prints out all the numeric objects that are currently defined.
How metaprogramming and reflection enhance SOA
Service-oriented Architecture is enhanced when the underlying programs are able to dynamically adapt to the data received. Metaprogramming and Reflection make this possible. To see how this is so, it will be helpful to look at a few potential uses.
One such use would be a service that writes code based on the inputs. The services within SOA make use of the data passed to them. When the passed data represents code, for example 'print "Hello World"' in a language independent way, the receiving service could use metaprogramming to write code to just that. The service would then use reflection to determine what methods are available, in our case 'print', and would perform that task. If the service logic was to "repeat 3 times", then the code would ultimately print "Hello World" that number of times. XML may be used to transfer this message as follows:
- Example XML Data:
<message> <method>print</method> Hello World </message>
A more practical use of reflection and metaprogramming in a SOA would be a service that checks code for security holes. The code could be sent to the service via whatever protocol is chosen. This service could use reflection to examine the code. Next, the service would evaluate the code for common security flaws. If the program is determined to be safe enough, the service may run the code in a safe environment and perform further testing. The service would output the results of the analysis, and perhaps even output an auto-corrected program upon completion.
Compilers are a good current illustration of metaprogramming and reflection working in a service. The compiler may be viewed as a service within a SOA. It can be run on many different systems, which is a major benefit of SOA's. Data (the code) is sent to a service (the compiler) which then translates it to machine code for the particular system. Metaprogramming is at work here, because each line of code is generally used to write one or more lines of machine code (code directly executable by the computer). The code and compiler effectively write a new program in machine code. Reflection may take place when symbols generated by one section of code are referenced by another section of code. 11
A pattern emerges when studying these examples. Metaprogramming and reflection are generally used within the service to enhance the capability of the service. The data passed to the service is often what guides how metaprogramming and reflection take place.
Conclusion
SOA is an evolution of distributed computing based on web services. It is an attractive solution for companies since it is platform independent allowing different software and hardware to communicate together easily.
Metaprogramming and reflection are programming styles used to take advantage of the features in dynamic programming languages. They provide a great advantage when used in the development of SOAs to allow the services to dynamically change and adapt to the messages being passed between services.
References
1. http://www.service-architecture.com/web-services/articles/service-oriented_architecture_soa_definition.html - Brief explanation of SOA
2. Thomas, Dave (2006). Programming Ruby, The Pragmatic Programmers' Guide.
3. http://en.wikipedia.org/wiki/Service_oriented_architecture - A very detailed explanation of SOA
4. http://www.javaworld.com/javaworld/jw-06-2005/jw-0613-soa.html - A more easy to understand explanation of SOA
5. http://en.wikipedia.org/wiki/Reflection_(computer_science) - Explanation of reflection
6. http://en.wikipedia.org/wiki/Metadata - Explanation of metadata
7. http://en.wikipedia.org/wiki/Metaprogramming - Explanation of metaprogramming
8. http://www.ruby-doc.org/core/classes/ObjectSpace.html - Explanation of the ObjectSpace command in Ruby
9. http://weare.buildingsky.net/2009/08/25/rubys-metaprogramming-toolbox - Explanation of tools that Ruby provides for metaprogramming
10. Perrotta, Paolo (2009). Metaprogramming Ruby.
11. http://dvanderboom.wordpress.com/2008/11/06/programming-language-directions/ - Illustrates the use of a compiler as a service