Changeset 6029

Show
Ignore:
Timestamp:
11/09/06 17:28:30
Author:
jan
Message:

full sample includes, more doc

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/Rakefile

    r6028 r6029  
    1515  s.platform = Gem::Platform::RUBY 
    1616  s.required_ruby_version = '>= 1.8.4' 
    17   s.summary = 'Jerbil java build system
     17  s.summary = 'Java build system, based on rake
    1818  s.name = 'jerbil' 
    1919  s.homepage = 'http://code.trampolinesystems.com/jerbil' 
     
    2323  s.require_path = 'lib' 
    2424  s.requirements << 'rjb' 
     25  s.requirements << 'rake' 
    2526  s.requirements << 'JDK 5.0' 
    26   files = FileList['lib/**/*', 'test/*.rb', 'buildsupport/**/*', 'classloader/*', 'COPYING', 'ChangeLog', 'README'] 
    27   s.has_rdoc = fals
     27  files = FileList['lib/**/*', 'test/*.rb', 'buildsupport/**/*', 'classloader/*', 'sample/**/*', 'COPYING', 'ChangeLog', 'README'] 
     28  s.has_rdoc = tru
    2829  s.files = files 
    2930  s.test_files = FileList['test/*.rb'] 
     
    4243  rd.main = "README" 
    4344  rd.rdoc_files.include("README", "lib/**/*.rb") 
     45  rd.options << "--inline-source" 
    4446end 
    4547   
     
    6971  end 
    7072end 
     73 
     74desc "publish documentation" 
     75task :publish_doc  do |t| 
     76  require 'net/sftp' 
     77  Net::SFTP.start("trampolinesystems.com") do |s| 
     78    #s.put_file(File.join(Rake.original_dir,'Rakefile'), '/tmp/R') 
     79    #s.mkdir('/tmp/moin') 
     80  end 
     81end 
  • trunk/lib/jerbil.rb

    r6010 r6029  
    1 #$:.unshift File.join( File.dirname(__FILE__),  "lib" ) 
    2  
    31require 'jerbil/java_helper' 
    42require 'jerbil/javac_task' 
  • trunk/lib/jerbil/apt_task.rb

    r6008 r6029  
    44require File.dirname(__FILE__) + '/javac_task' 
    55 
    6 # Apt helper class - uses annotation processor API to identify annotations. 
    7 module Rake 
     6module Jerbil 
     7 
     8  # A task using Java's {Annotation Processing Tool}[http://java.sun.com/j2se/1.5.0/docs/guide/apt/] 
     9  # (apt) to compile (optional) and process annotations found in the class files. 
     10  # 
     11  # This task can be used to gather a list of all EJB3 entities, or testng annotated classes 
     12  # during the compile step. 
     13  # 
     14  # == Example 
     15  # 
     16  #  desc "compile all java files and find annotations" 
     17  #  Jerbil::AptTask.new(:compile) do |t| 
     18  #    t.java_files = Jerbil::JavaFileList.new("src", "classes") 
     19  #    t.find_annotation 'javax.persistence.Entity' do |entities| 
     20  #       File.open(ANNOTATED_CLASSES, 'w') { |f| f << entities.to_yaml } 
     21  #    end 
     22  #  end 
    823  class AptTask < JavacTask 
    924 
     
    1934    end 
    2035         
    21     def supportedOptions 
     36     
     37    def supportedOptions # :nodoc: 
    2238      empty_list 
    2339    end 
    2440 
    25     def supportedAnnotationTypes 
     41    def supportedAnnotationTypes # :nodoc: 
    2642      list = empty_list 
    2743      annotations.each_key { |a| list.add a.to_s } 
     
    2945    end 
    3046     
    31     def getProcessorFor(set,env) 
     47    def getProcessorFor(set,env) # :nodoc: 
    3248      @env = env 
    3349      Rjb::bind(self, 'com.sun.mirror.apt.AnnotationProcessor') 
    3450    end 
    3551     
    36     def hashCode 
     52    def hashCode # :nodoc: 
    3753      123456 
    3854    end  
     
    4662        end 
    4763      end    
     64    end 
     65     
     66    # Registers an annotation handler for all annotations with name +annotation+. 
     67    # Handlers (code blocks) will be invoked after successful compilation, in #post_compile. 
     68    # See AptTask for an example. 
     69    def find_annotation(annotation, options={}, &handler) 
     70      defopts = { :scope => :class } 
     71      defopts.merge! options 
     72      annotations[annotation] = Proc.new { |spec| 
     73           t = spec.getDeclaringType || spec 
     74           @found_annotations[annotation] ||= [] 
     75           @found_annotations[annotation] << t.toString 
     76      } 
     77      @found_annotations_handler[annotation] = handler 
     78    end 
     79     
     80    # A convenience method to serialize a list of found annotations  
     81    # to +filename+ using yaml. 
     82    def annotated_classes_to_yaml(annotation, filename) 
     83      find_annotation annotation do |classes| 
     84        File.open(filename, 'w') { |f| f << classes.to_a.uniq.to_yaml } 
     85      end 
     86    end 
     87         
     88    protected    
     89    def compile(parameters, printwriter) 
     90      apt = Rjb::import('com.sun.tools.apt.Main') 
     91      parameters << "-nocompile" if nocompile 
     92      apt.process(get_processor_factory, printwriter, parameters ) 
    4893    end 
    4994     
     
    60105        end 
    61106      end 
    62     end 
    63      
    64     def find_annotation(annotation, options={}, &handler) 
    65       defopts = { :scope => :class } 
    66       defopts.merge! options 
    67       annotations[annotation] = Proc.new { |spec| 
    68            t = spec.getDeclaringType || spec 
    69            @found_annotations[annotation] ||= [] 
    70            @found_annotations[annotation] << t.toString 
    71       } 
    72       @found_annotations_handler[annotation] = handler 
    73     end 
    74      
    75     def annotated_classes_to_yaml(annotation, filename) 
    76       find_annotation annotation do |classes| 
    77         File.open(filename, 'w') { |f| f << classes.to_a.uniq.to_yaml } 
    78       end 
    79     end 
    80          
    81     protected    
    82     def compile(parameters, printwriter) 
    83       apt = Rjb::import('com.sun.tools.apt.Main') 
    84       parameters << "-nocompile" if nocompile 
    85       apt.process(get_processor_factory, printwriter, parameters ) 
    86107    end 
    87108     
  • trunk/lib/jerbil/hibernate_task2.rb

    r6008 r6029  
    55 
    66 
    7 module Rake  
    8   module HibernateFoo  
    9     class ExportSchemaTask < TaskLib 
     7module Jerbil  
     8  module Hibernate  
     9    # Generate a sql schema from EJB3-annotated classes. 
     10    # 
     11    # == Example 
     12    #   Jerbil::Hibernate::ExportSchemaTask.new(:export_schema) do |t| 
     13    #       t.schemafile = "schema.sql" 
     14    #       t.persistencefile = PERSISTENCE_YML       
     15    #   end 
     16    class ExportSchemaTask < Rake::TaskLib 
    1017      include JavaHelper 
    1118       
     
    6067          File.open(schemafile, "w") {|file| file << schema } 
    6168        end 
     69        file schemafile => name 
     70        task name => persistencefile 
    6271      end   
    6372    end 
    6473   
     74    # Turns the hibernate sql output into something readable. Only tested 
     75    # with the mysql dialect. 
    6576    class SqlBeautifier 
    6677      def SqlBeautifier.beautify(sql) 
     
    91102     end 
    92103      
     104     # Wrapper class around _org.hibernate.tool.hbm2ddl.SchemaExport_. 
    93105     class SchemaExporter 
    94106       def initialize(classes, outputfile, dialect, package=nil) 
     
    123135           
    124136          pkg = clazz.getPackage 
    125           packages << pkg.getName if pkg.getAnnotations.length > 0 
     137          packages << pkg.getName if pkg && pkg.getAnnotations.length > 0 
    126138        end 
    127139        packages << package unless package.nil? 
  • trunk/lib/jerbil/jar_task.rb

    r6008 r6029  
    22require 'rake/tasklib' 
    33 
    4 module Rake 
    5   class JarTask < TaskLib 
     4module Jerbil 
     5  # A task to create jar files. 
     6  # 
     7  # == Example 
     8  #   Jerbil::JarTask.new do |t| 
     9  #     t.dir = JAVA_BUILD_DIR 
     10  #     t.filename = DISTJAR 
     11  #     t.depends_on :clean, :compile 
     12  #   end 
     13  class JarTask < Rake::TaskLib 
    614    include JavaHelper 
    715   
  • trunk/lib/jerbil/java_helper.rb

    r6019 r6029  
    1616$DIR_SEP_FOR_SUBSTITUTION = $IS_WINDOWS ? "\\\\" : "/" 
    1717 
    18 module JavaHelper 
    19  
    20   # Provides the block with an instance of java.io.PrintStream and returns 
    21   # all text printed to it as ruby-typed string. 
    22   def printStream_to_s(&block) # :yields: printstream 
    23     yieldIO('java.io.PrintStream', block) 
    24   end 
    25   
    26    
    27   # Provides the block with an instance of java.io.PrintWriter and returns 
    28   # all text printed to it as ruby-typed string. 
    29   def printWriter_to_s(&block) # :yields: printwriter 
    30     yieldIO('java.io.PrintWriter', block) 
    31   end 
    32    
    33   def yieldIO(klass, block) # :nodoc: 
    34     out = Rjb::import('java.io.ByteArrayOutputStream').new 
    35     ps = Rjb::import(klass.to_s).new_with_sig 'Ljava.io.OutputStream;', out     
    36     block.call(ps) 
    37     ps.flush 
    38     st = Rjb::import('java.lang.String').new_with_sig '[B', out.toByteArray 
    39     String.new(st.toString) 
    40   end 
    41    
    42   # Returns an empty instance of <em>java.util.List</em>. 
    43   def empty_list 
    44     Rjb::import('java.util.ArrayList').new 
    45   end 
    46    
    47   def str_list(strings) 
    48     l = empty_list 
    49     strings.each { |s| l.add s.to_s } 
    50     l 
    51   end 
    52    
    53   def serialize(obj) 
    54     byteoos = Rjb::import('java.io.ByteArrayOutputStream').new 
    55     oosKlass = Rjb::import('java.io.ObjectOutputStream') 
    56     oos = oosKlass.new_with_sig 'Ljava.io.OutputStream;', byteoos 
    57     begin 
    58       oos.writeObject(obj) 
    59     ensure 
    60       oos.close 
     18module Jerbil 
     19 
     20  # The JavaHelper module provides common helper functionality needed across different 
     21  # classes. 
     22  module JavaHelper 
     23   
     24    # Provides the block with an instance of java.io.PrintStream and returns 
     25    # all text printed to it as ruby-typed string. 
     26    def printStream_to_s(&block) # :yields: printstream 
     27      yieldIO('java.io.PrintStream', block) 
     28    end 
     29    
     30     
     31    # Provides the block with an instance of java.io.PrintWriter and returns 
     32    # all text printed to it as ruby-typed string. 
     33    def printWriter_to_s(&block) # :yields: printwriter 
     34      yieldIO('java.io.PrintWriter', block) 
     35    end 
     36     
     37    def yieldIO(klass, block) # :nodoc: 
     38      out = Rjb::import('java.io.ByteArrayOutputStream').new 
     39      ps = Rjb::import(klass.to_s).new_with_sig 'Ljava.io.OutputStream;', out     
     40      block.call(ps) 
     41      ps.flush 
     42      st = Rjb::import('java.lang.String').new_with_sig '[B', out.toByteArray 
     43      String.new(st.toString) 
     44    end 
     45     
     46    # Returns an empty instance of <em>java.util.List</em>. 
     47    def empty_list 
     48      Rjb::import('java.util.ArrayList').new 
     49    end 
     50     
     51    # Returns an instance of java.util.List, populated with all 
     52    # string found in +strings+. 
     53    def str_list(strings) 
     54      l = empty_list 
     55      strings.each { |s| l.add s.to_s } 
     56      l 
     57    end 
     58     
     59    # Serializes +obj+ using standard Java serialization. The result 
     60    # is returned as bytearray. 
     61    def serialize(obj) 
     62      byteoos = Rjb::import('java.io.ByteArrayOutputStream').new 
     63      oosKlass = Rjb::import('java.io.ObjectOutputStream') 
     64      oos = oosKlass.new_with_sig 'Ljava.io.OutputStream;', byteoos 
     65      begin 
     66        oos.writeObject(obj) 
     67      ensure 
     68        oos.close 
     69      end  
     70      byteoos.toByteArray 
     71    end 
     72     
     73    # Loads the java virtual machine. This method should only be invoked once, typically 
     74    # before task definitions in a Rakefile. 
     75    # 
     76    # +classpath+:: a Rake::FileList containing the initial classpath.  
     77    # +build_dir+:: an optional directory (or list of directories) which will be used to resolve classes at runtime. 
     78    # +loggingprops+:: the location of a java.util.logging configuration file. 
     79    def load_jvm(classpath, build_dir = nil, loggingprops = nil )  
     80      #need verbose java exceptions 
     81      $VERBOSE = true 
     82     
     83      #include tools.jar from JDK (needed for javac etc.) 
     84      java_home = ENV['JAVA_HOME'] 
     85      classpath.include(File.join(java_home, "lib", "tools.jar")) if java_home     
     86      #include build jars and custom classloader 
     87      classpath.include(File.join(File.dirname(__FILE__), "../../buildsupport/*.jar")) 
     88      classpath.include(File.join(File.dirname(__FILE__), "../../classloader")) unless build_dir.nil? 
     89       
     90      jvmargs = []     
     91      jvmargs << "-Djava.util.logging.config.file=#{loggingprops.to_s}" unless loggingprops.nil?  
     92   
     93      if $JAVA_DEBUG 
     94        jvmargs += [ 
     95        "-Xdebug", 
     96        "-Xnoagent", 
     97        "-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n" ] 
     98      end 
     99       
     100      #java_home = ENV['JAVA_HOME']     
     101      #ENV['LD_LIBRARY_PATH'] = "#{java_home}/jre/lib/i386:#{java_home}/jre/lib/i386/client" 
     102       
     103      #puts "lib:" + ENV['LD_LIBRARY_PATH'] 
     104      #jvmargs << "-Djava.library.path=#{ENV['JAVA_HOME']}/jre/lib/i386" 
     105       
     106      if build_dir 
     107        jvmargs += [ "-Djava.system.class.loader=JerbilClassLoader",  
     108          "-Djerbil.build.root=#{build_dir.to_a.join(':')}", "-Djerbil.debug=false" ]  
     109      else       
     110        $stderr << "jerbil: build_dir not set: dynamic classloading is disabled\n" if Rake.application.options.trace 
     111      end 
     112            
     113      begin 
     114        Rjb::load(classpath.to_cp, jvmargs) 
     115      rescue  
     116        $stderr << "could not load java vm: make sure JAVA_HOME is set correctly!\n" 
     117        raise 
     118      end 
     119      
     120      #TODO: test javac main and raise if not found 
     121    end 
     122  end 
     123   
     124  # Tasks including this module can specifiy additional 
     125  # Java style arguments (like -verbose, -gc). 
     126  #  
     127  # == Example 
     128  # 
     129  #     Jerbil::MyExtraArgumentTakingTask.new do |t| 
     130  #       t.foo = 'baz' 
     131  #       t.options :wibble, :wobble 
     132  #     end 
     133  # 
     134  # results in a command line of the form "-foo baz -wibble -wobble".   
     135  module ExtraArgumentTaking       
     136      def self.append_features(base) 
     137        super          
     138        class << base         
     139          def create_alias_for(actual, new) 
     140            @@aliases ||= {} 
     141            @@aliases[new.to_s] = actual.to_s          
     142          end      
     143        end 
     144      end 
     145         
     146      attr_reader :extra_args 
     147       
     148      def options(*args) 
     149        args.each {|a| self.send(a)} 
     150      end 
     151       
     152      def add_files(files) 
     153        add_extra_args files.to_a 
     154      end 
     155             
     156      def add_extra_args(*args) 
     157        @extra_args = [] if extra_args.nil? 
     158        @extra_args += args.flatten 
     159      end 
     160              
     161      def method_missing(symbol, *args)    
     162        arg = symbol.to_s.sub(/=/, "") 
     163        if @@aliases && @@aliases.has_key?(arg)    
     164          arg = @@aliases[arg]   
     165        end 
     166        add_extra_args "-#{arg}", args 
     167      end 
     168  end 
     169   
     170  # A JavaFileList is a specialisation of a standard Rake::FileList. 
     171  # It includes additional methods to deal with build dirs, resources  
     172  # and other java specifics. 
     173  class JavaFileList < Rake::FileList 
     174    attr_reader :srcdir 
     175    attr_reader :dstdir 
     176    attr_accessor :resource_patterns 
     177     
     178    # srcdir:: the source dir 
     179    # dstdir:: destination directory for class files (used by JavacTask). 
     180    def initialize(srcdir, dstdir, extensions = nil) 
     181      super([]) 
     182      @srcdir = srcdir 
     183      @dstdir = dstdir 
     184      @resource_patterns = [] 
     185      copy_extensions = extensions || [ "xml", "properties" ]  
     186      copy_extensions.each { |ext| add_extension(ext) }  
     187      include(srcdir + "/**/*.java") 
     188    end 
     189     
     190    # Returns a list of all java source files formatted as java class names. 
     191    # For example src/org/foo/Baz -> org.foo.Baz. 
     192    def to_classnames 
     193      # remove the initial directory and separator 
     194      sub = srcdir + $DIR_SEP_FOR_SUBSTITUTION 
     195      paths = self.pathmap("%{^#{sub},}X") 
     196       
     197      paths.gsub!($DIR_SEP, ".") 
     198      paths.gsub!("/", "." ) 
     199    end 
     200     
     201    # Returns a list of Java classes. This method uses Rjb::import to 
     202    # load the specified classes into the virtual machine. 
     203    def to_classes 
     204      classnames = to_classnames 
     205      classes = classnames.map {|name| Rjb::import(name)} 
     206      classes.to_a 
     207    end 
     208     
     209    # Translates all java source files into their corresponding class destination 
     210    # files, based on +dstdir+. 
     211    # 
     212    # For example src/org/foo/Baz.java -> classes/org/foo/Baz.class. 
     213    def to_classfiles 
     214      self.pathmap("%{^#{srcdir},#{dstdir}}X.class") 
     215    end 
     216     
     217    def sourcepath 
     218      srcdir 
     219    end 
     220     
     221    # Returns a Rake::FileList containing all resources found in +srcdir+. 
     222    # Resources are typically files in +srcdir+ with extensions other than .java  
     223    # (properties, xml, ...) 
     224    def resources 
     225      r = FileList.new 
     226      resource_patterns.each { |p| r.include(srcdir+p) } 
     227      r 
    61228    end  
    62     byteoos.toByteArray 
    63   end 
    64    
    65   def load_vm(classpath, build_dir = nil, loggingprops = nil )  
    66     #need verbose java exceptions 
    67     $VERBOSE = true 
    68    
    69     #include tools.jar from JDK (needed for javac etc.) 
    70     java_home = ENV['JAVA_HOME'] 
    71     classpath.include(File.join(java_home, "lib", "tools.jar")) if java_home     
    72     #include build jars and custom classloader 
    73     classpath.include(File.join(File.dirname(__FILE__), "../../buildsupport/*.jar")) 
    74     classpath.include(File.join(File.dirname(__FILE__), "../../classloader")) unless build_dir.nil? 
    75      
    76     jvmargs = []     
    77     jvmargs << "-Djava.util.logging.config.file=#{loggingprops.to_s}" unless loggingprops.nil?  
    78  
    79     if $JAVA_DEBUG 
    80       jvmargs += [ 
    81       "-Xdebug", 
    82       "-Xnoagent", 
    83       "-Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n" ] 
    84     end 
    85      
    86     #java_home = ENV['JAVA_HOME']     
    87     #ENV['LD_LIBRARY_PATH'] = "#{java_home}/jre/lib/i386:#{java_home}/jre/lib/i386/client" 
    88      
    89     #puts "lib:" + ENV['LD_LIBRARY_PATH'] 
    90     #jvmargs << "-Djava.library.path=#{ENV['JAVA_HOME']}/jre/lib/i386" 
    91      
    92     if build_dir 
    93       jvmargs += [ "-Djava.system.class.loader=JerbilClassLoader",  
    94         "-Djerbil.build.root=#{build_dir.to_a.join(':')}", "-Djerbil.debug=false" ]  
    95     else       
    96       $stderr << "jerbil: build_dir not set: dynamic classloading is disabled\n" if Rake.application.options.trace 
    97     end 
    98           
    99     begin 
    100         Rjb::load(classpath.to_cp, jvmargs) 
    101     rescue  
    102             $stderr << "could not load java vm: make sure JAVA_HOME is set correctly!\n" 
    103       raise 
     229     
     230    # Calls block once for each resource found in +srcdir+, passing 
     231    # the source and destination file as parameter. 
     232    def resources_and_target 
     233      resources.each do | r | 
     234        target =  r.sub(/#{srcdir}/, dstdir) 
     235        yield r, target if block_given? 
     236      end 
     237    end 
     238     
     239    # Registers a resource extension. 
     240    def add_extension(ext) 
     241      @resource_patterns << "/**/*.#{ext}" 
     242    end 
     243     
     244    # Registers a list of extensions. 
     245    def add_extensions(exts) 
     246      @resource_patterns.concat(exts.to_a) 
     247    end 
     248   
     249    def dump(files) 
     250      files.each do |f| 
     251        print f + "\n" 
     252      end 
     253    end 
     254  end 
     255   
     256  # A MultiJavaFileList is a container object for holding several JavaFileList 
     257  # objects. This is useful for multidirectory builds. 
     258  class MultiJavaFileList 
     259   
     260    attr_reader :modules, :dstdir 
     261     
     262    def initialize(modules, dstdir, srcprefix = "src", copypat = nil)  
     263      @java_files = [] 
     264      @modules = modules 
     265      @dstdir = dstdir 
     266      modules.each do | m | 
     267        srcdir = File.join(m, srcprefix) 
     268        @java_files << JavaFileList.new(srcdir, dstdir, copypat ) 
     269      end 
     270    end 
     271     
     272    def sourcepath 
     273      @java_files.collect{|jf| jf.srcdir}.join($JAVA_PATH_SEPERATOR) 
     274    end 
     275     
     276    def srcdir 
     277      @java_files.collect{|jf| jf.srcdir} 
     278    end 
     279     
     280    def to_a 
     281      files = [] 
     282      @java_files.each do |f| 
     283        files += f 
     284      end 
     285      files 
     286    end 
     287     
     288    def to_ary 
     289      to_a 
     290    end   
     291     
     292    def resources 
     293      res = [] 
     294      @java_files.each do |f| 
     295        res += f.resources   
     296      end 
     297      res 
     298    end 
     299     
     300    def resources_and_target 
     301      @java_files.each do |jf| 
     302        jf.resources_and_target do |r,t| 
     303          yield r,t 
     304        end 
     305      end 
     306    end 
     307     
     308    def gsub!( replace, replace_with ) 
     309      @java_files.each{ |f| f.gsub!( replace, replace_with ) } 
     310    end 
     311  end 
     312end 
     313 
     314module Rake 
     315  class FileList   
     316    # Returns the filelist formatted as Java classpath. 
     317    # ("/tmp/foo.jar:/tmp/baz.jar") 
     318    def to_cp   
     319      self.join($JAVA_PATH_SEPERATOR) 
     320    end 
     321  end 
     322   
     323  class TaskLib   
     324    # Adds a dependency to the given task 
     325    def depends_on(*args) 
     326      dependencies.concat(args) 
    104327    end 
    105328    
    106     #TODO: test javac main and raise if not found 
     329    def dependencies 
     330      @dependencies ||= [] 
     331    end 
    107332  end 
    108333end 
    109334 
    110 # Tasks including this module can specifiy additional 
    111 # java style arguments. 
    112 #  
    113 
    114 module ExtraArgumentTaking       
    115     def self.append_features(base) 
    116       super          
    117       class << base         
    118         def create_alias_for(actual, new) 
    119           @@aliases ||= {} 
    120           @@aliases[new.to_s] = actual.to_s          
    121         end      
    122       end 
    123     end 
    124        
    125     attr_reader :extra_args 
    126      
    127     def options(*args) 
    128       args.each {|a| self.send(a)} 
    129     end 
    130      
    131     def add_files(files) 
    132       add_extra_args files.to_a 
    133     end 
    134            
    135     def add_extra_args(*args) 
    136       @extra_args = [] if extra_args.nil? 
    137       @extra_args += args.flatten 
    138     end 
    139             
    140     def method_missing(symbol, *args)    
    141       arg = symbol.to_s.sub(/=/, "") 
    142       if @@aliases && @@aliases.has_key?(arg)    
    143         arg = @@aliases[arg]   
    144       end 
    145       add_extra_args "-#{arg}", args 
    146     end 
    147 end 
    148  
    149 class Rake::FileList   
    150   def to_cp   
    151     self.join($JAVA_PATH_SEPERATOR) 
    152   end 
    153 end 
    154  
    155 class Rake::TaskLib   
    156   def depends_on(*args) 
    157     dependencies.concat(args) 
    158   end 
    159    
    160   def dependencies 
    161     @dependencies ||= [] 
    162   end 
    163 end 
    164  
    165 class JavaFileList < Rake::FileList 
    166   attr_reader :srcdir 
    167   attr_reader :dstdir 
    168   attr_accessor :resource_patterns 
    169    
    170   def initialize(srcdir, dstdir, extensions = nil) 
    171     super([]) 
    172     @srcdir = srcdir 
    173     @dstdir = dstdir 
    174     @resource_patterns = [] 
    175     copy_extensions = extensions || [ "xml", "properties" ]  
    176     copy_extensions.each { |ext| add_extension(ext) }  
    177     include(srcdir + "/**/*.java") 
    178   end 
    179    
    180   def to_classnames 
    181         # remove the initial directory and separator 
    182         sub = srcdir + $DIR_SEP_FOR_SUBSTITUTION 
    183         paths = self.pathmap("%{^#{sub},}X") 
    184          
    185         paths.gsub!($DIR_SEP, ".") 
    186         paths.gsub!("/", "." ) 
    187   end 
    188    
    189   def to_classes 
    190     classnames = to_classnames 
    191     classes = classnames.map {|name| Rjb::import(name)} 
    192     classes.to_a 
    193   end 
    194    
    195   def to_classfiles 
    196     self.pathmap("%{^#{srcdir},#{dstdir}}X.class") 
    197   end 
    198    
    199   def sourcepath 
    200     srcdir 
    201   end 
    202    
    203   def resources 
    204     r = FileList.new 
    205     resource_patterns.each { |p| r.include(srcdir+p) } 
    206     r 
    207   end  
    208    
    209   def resources_and_target 
    210     resources.each do | r | 
    211       target =  r.sub(/#{srcdir}/, dstdir) 
    212       yield r, target if block_given? 
    213     end 
    214   end 
    215    
    216   def add_extension(ext) 
    217     @resource_patterns << "/**/*.#{ext}" 
    218   end 
    219    
    220   def add_extensions(exts) 
    221     @resource_patterns.concat(exts.to_a) 
    222   end 
    223  
    224   def dump(files) 
    225         files.each do |f| 
    226                 print f + "\n" 
    227         end 
    228   end 
    229 end 
    230  
    231 class MultiJavaFileList 
    232  
    233   attr_reader :modules, :dstdir 
    234    
    235   def initialize(modules, dstdir, srcprefix = "src", copypat = nil)  
    236     @java_files = [] 
    237     @modules = modules 
    238     @dstdir = dstdir 
    239     modules.each do | m | 
    240       srcdir = File.join(m, srcprefix) 
    241       @java_files << JavaFileList.new(srcdir, dstdir, copypat ) 
    242     end 
    243   end 
    244    
    245   def sourcepath 
    246     @java_files.collect{|jf| jf.srcdir}.join($JAVA_PATH_SEPERATOR) 
    247   end 
    248    
    249   def srcdir 
    250     @java_files.collect{|jf| jf.srcdir} 
    251   end 
    252    
    253   def to_a 
    254     files = [] 
    255     @java_files.each do |f| 
    256       files += f 
    257     end 
    258     files 
    259   end 
    260    
    261   def to_ary 
    262     to_a 
    263   end   
    264    
    265   def resources 
    266     res = [] 
    267     @java_files.each do |f| 
    268       res += f.resources   
    269     end 
    270     res 
    271   end 
    272    
    273   def resources_and_target 
    274     @java_files.each do |jf| 
    275       jf.resources_and_target do |r,t| 
    276         yield r,t 
    277       end 
    278     end 
    279   end 
    280    
    281   def gsub!( replace, replace_with ) 
    282     @java_files.each{ |f| f.gsub!( replace, replace_with ) } 
    283   end 
    284 end 
    285  
     335 
  • trunk/lib/jerbil/java_task.rb

    r6008 r6029  
    22require 'rake/tasklib' 
    33 
    4 module Rake 
    5   class JavaTask < TaskLib 
     4module Jerbil 
     5  # Runs a Java program, either in-vm or as separate process (forked or replacing 
     6  # current process). 
     7  # 
     8  # == Example (forked) 
     9  #     Jerbil::JavaTask.new(:run, "jerbil.sample.Main") do |t| 
     10  #       t.classpath = CLASSPATH 
     11  #       t.parameters = [ "-foo", "baz" ]        
     12  #       t.sys_property "jerbil.foo", "baz" 
     13  #       t.fork = true 
     14  #       t.depends_on :compile 
     15  #     end 
     16  # == Example (in-vm) 
     17  #     Jerbil::JavaTask.new(:run, "jerbil.sample.Main") do |t| 
     18  #       t.parameters = [ "-foo", "baz" ]        
     19  #       t.in_vm = true      
     20  #       t.depends_on :compile 
     21  #     end 
     22   
     23  class JavaTask < Rake::TaskLib 
    624     include ExtraArgumentTaking 
    725      
     
    1230     attr_accessor :classname 
    1331      
    14      # program args 
     32     # Program args 
    1533     attr_accessor :parameters 
    1634          
     
    3149     end 
    3250        
    33      def yourkit(port=1001, platform="linux-x86-32") 
     51     def yourkit(port=1001, platform="linux-x86-32") # :nodoc: 
    3452        add_extra_args "-agentlib:yjpagent=port=#{port}" 
    3553        ENV['LD_LIBRARY_PATH'] = ":./lib/yourkit/#{platform}" 
    3654     end 
    3755      
     56     # Adds a system property to the command line. 
    3857     def sys_property(name,value) 
    3958        add_extra_args "-D#{name}=#{value}" 
     
    4463     end 
    4564      
     65     # The max. amount of memory (in MB) the new Java process is allowed to 
     66     # take. 
    4667     def max_mem=(mem) 
    4768        add_extra_args "-Xmx#{mem}M" 
    4869     end 
    4970      
     71     # Runs program in debug mode, listening on port +port+. 
    5072     def debug(port=8000, suspend="n") 
    5173        add_extra_args "-Xdebug", "-Xnoagent", 
  • trunk/lib/jerbil/javac_task.rb

    r6008 r6029  
    33require File.dirname(__FILE__) + '/java_helper' 
    44 
    5  
    6  
    7 module Rake 
     5module Jerbil 
    86  # == Example 
    97  # 
    108  #  desc "compile all java files" 
    11   #  Rake::JavacTask.new(:compile) do |t| 
    12   #    t.java_files = JAVA_FILES 
     9  #  Jerbil::JavacTask.new(:compile) do |t| 
     10  #    t.java_files = Jerbil::JavaFileList.new("src", "classes") 
    1311  #    t.options :nowarn, :debug 
    1412  #  end 
    15   class JavacTask < TaskLib 
     13  class JavacTask < Rake::TaskLib 
    1614    include JavaHelper, ExtraArgumentTaking 
    1715     
  • trunk/lib/jerbil/javadoc_task.rb

    r6008 r6029  
    22require 'rake/tasklib' 
    33 
    4 module Rake 
    5   class JavaDocTask < TaskLib 
     4module Jerbil 
     5  # A task to create javadoc from source files. 
     6  # == Example 
     7  #   Jerbil::JavaDocTask.new do |t| 
     8  #     t.sourcepath = SOURCE_DIR 
     9  #     t.subpackages = "jerbil" 
     10  #     t.dstdir = JAVADOC_DIR 
     11  #     t.options :quiet   
     12  #     t.depends_on :compile 
     13  #   end 
     14  class JavaDocTask < Rake::TaskLib 
    615    include ExtraArgumentTaking 
    716     
    817     attr_accessor :name 
     18     # Desstionation directory for javadocs. 
    919     attr_accessor :dstdir 
    1020           
  • trunk/lib/jerbil/jibx_task.rb

    r6008 r6029  
    33require File.dirname(__FILE__) + '/java_helper' 
    44 
    5 module Rake 
    6   class JibxTask < TaskLib 
     5module Jerbil 
     6  # Compiles JIBX bindings. 
     7  # 
     8  # == Example 
     9  #     Jerbil::JibxTask.new do |t| 
     10  #       t.bindings = FileList[File.join(JAVA_BUILD_DIR, '/**/jibxModelExternalisedBinding.xml')] 
     11  #       t.classpath = CLASSPATH 
     12  #     end 
     13  class JibxTask < Rake::TaskLib 
    714    include JavaHelper 
    815     
  • trunk/lib/jerbil/testng_task.rb

    r6008 r6029  
    44require File.dirname(__FILE__) + '/java_helper' 
    55 
    6 module Rake 
     6module Jerbil 
    77  module TestNG 
     8    # A Task to run testng tasks. 
     9    # 
     10    # == Example 
     11    # 
     12    #   Jerbil::TestNG::TestNGTask.new(:test) do |t| 
     13    #     t.outputdir = TESTOUTPUTDIR 
     14    #     t.tests     = JAVA_TEST_FILES 
     15    #     t.depends_on :test_compile 
     16    #   end 
     17    # 
    818    class TestNGTask < Rake::TaskLib 
    919      include JavaHelper 
     
    3646          testng = Rjb::import('org.testng.TestNG').new_with_sig 'Z', false 
    3747          
    38           tl = Rake::TestNG::TestListener.new 
    39           sl = Rake::TestNG::SuiteListener.new 
     48          tl = TestListener.new 
     49          sl = SuiteListener.new 
    4050           
    4151          #need to use _invoke because addListener has 3 different method signatures 
     
    7585    end 
    7686     
     87    # A TestNG test listener imlemented in ruby. It mimics Ruby's standard testrunner. 
    7788    class TestListener 
    7889      include JavaHelper 
     
    123134        end 
    124135      
     136        # Returns a list of all failed classes. 
    125137        def failed_to_s 
    126138          @failed_classes.to_a.join(', ')    
     
    144156      end 
    145157       
     158      # A Ruby implementation of a TestNG suite listener. 
    146159      class SuiteListener 
    147160        def onStart(suite) 
     
    157170     
    158171      class << self 
     172       
     173        # Helper method to create a testng xml suite file. 
     174        # +filename+:: destination file for suite file. 
     175        # +classnames+:: a list of test class names. 
     176        # +suitename+:: name of the suite. 
     177        # +onetest++: whether all tests should be rolled into one. 
     178        # +excluded++: classes excluded from the test. 
    159179        def create_suite_xml(filename, classnames, suitename="default", onetest=false, excluded = []) 
    160180        
     
    187207                          
    188208        end # create_suite_xml  
    189        
    190          
    191         def write_excludes_includes(xml, excluded, included = [])            
     209             
     210        def write_excludes_includes(xml, excluded, included = []) # :nodoc:           
    192211              xml.groups do 
    193212                xml.run do 
  • trunk/sample/Rakefile

    r6027 r6029  
    55require 'buildconfig' 
    66 
    7 include JavaHelper 
     7include Jerbil::JavaHelper 
    88 
    9 load_vm(CLASSPATH, JAVA_BUILD_DIR) 
     9load_jvm(CLASSPATH, JAVA_BUILD_DIR) 
    1010 
    1111task :default => :compile 
    1212 
    1313desc "compile all java files" 
    14 Rake::JavacTask.new(:compile) do |t| 
     14Jerbil::JavacTask.new(:compile) do |t| 
    1515  t.java_files = JAVA_FILES 
    1616  t.options :nowarn, :debug 
     
    2020 
    2121desc "compile all tests" 
    22 Rake::JavacTask.new(:test_compile) do |t| 
     22Jerbil::JavacTask.new(:test_compile) do |t| 
    2323  t.java_files = JAVA_TEST_FILES 
    2424  t.depends_on :compile 
     
    2626 
    2727desc "run tests" 
    28 Rake::TestNG::TestNGTask.new(:test) do |t| 
     28Jerbil::TestNG::TestNGTask.new(:test) do |t| 
    2929  t.outputdir = TESTOUTPUTDIR 
    3030  t.tests     = JAVA_TEST_FILES 
     
    3232end 
    3333 
    34 Rake::JavaDocTask.new do |t| 
     34Jerbil::JavaDocTask.new do |t| 
    3535  t.sourcepath = SOURCE_DIR 
    3636  t.subpackages = "jerbil" 
     
    4040end 
    4141 
    42 Rake::JarTask.new do |t| 
     42Jerbil::JarTask.new do |t| 
    4343  t.dir = JAVA_BUILD_DIR 
    4444  t.filename = DISTJAR 
     
    4646end 
    4747 
    48 Rake::JavaTask.new(:run, "jerbil.sample.Main") do |t| 
     48Jerbil::JavaTask.new(:run, "jerbil.sample.Main") do |t| 
    4949  t.classpath = CLASSPATH 
    5050  t.parameters = [ "20", "50" ] 
     
    5252end 
    5353 
    54 Rake::JavaTask.new(:run_in_vm, "jerbil.sample.Main") do |t| 
     54Jerbil::JavaTask.new(:run_in_vm, "jerbil.sample.Main") do |t| 
    5555  t.classpath = CLASSPATH 
    5656  t.parameters = [ "20", "50" ] 
     
    5959end 
    6060 
    61 Rake::JavaTask.new(:run_forked, "jerbil.sample.Main") do |t| 
     61Jerbil::JavaTask.new(:run_forked, "jerbil.sample.Main") do |t| 
    6262  t.classpath = CLASSPATH 
    6363  t.parameters = [ "50", "-50" ] 
     
    6666end