Class: Jinx::ClassPathModifier

Inherits:
Object
  • Object
show all
Defined in:
lib/jinx/import/class_path_modifier.rb

Overview

Helper class which adds files to the Java class path.

Constant Summary collapse

WINDOWS_PATH_SEP =

The Windows semi-colon path separator.

';'
UNIX_PATH_SEP =

The Unix colon path separator.

':'

Instance Method Summary collapse

Instance Method Details

#add_to_classpath(file) ⇒ Object

Adds the given jar file or directory to the classpath.

Parameters:

  • file (String)

    the jar file or directory to add



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/jinx/import/class_path_modifier.rb', line 29

def add_to_classpath(file)
  unless File.exist?(file) then
    logger.warn("File to place on Java classpath does not exist: #{file}")
    return
  end
  if File.extname(file) == '.jar' then
    # require is preferred to classpath append for a jar file.
    require file
  else
    # A directory must end in a slash since JRuby uses an URLClassLoader.
    if File.directory?(file) then
      last = file[-1, 1]
      if last == "\\" then
        file = file[0...-1] + '/'
      elsif last != '/' then
        file = file + '/'
      end
    end
    # Append the file to the classpath.
    $CLASSPATH << file
  end
end

#expand_jars(directories) ⇒ <String> (private)

Expands the given directories to include the contained jar files. If a directory contains jar files, then the jar files are included in the resulting array. Otherwise, the directory itself is included in the resulting array.

Parameters:

  • directories (<String>)

    the directories containing jars to add

Returns:

  • (<String>)

    each directory or its jars



67
68
69
70
71
72
73
74
75
# File 'lib/jinx/import/class_path_modifier.rb', line 67

def expand_jars(directories)
  # If there are jar files, then the file list is the sorted jar files.
  # Otherwise, the file list is a singleton directory array.
  expanded = directories.map do |dir|
    jars = Dir[File.join(dir , "**", "*.jar")].sort
    jars.empty? ? [dir] : jars
  end
  expanded.flatten
end

#expand_to_class_path(path) ⇒ Object

Adds the directories in the given path and all Java jar files contained in the directories to the Java classpath.

in sort order. Java applications usually add jars in sort order. For examle, the Apache Ant directory-based classpath tasks are in sort order, although this is not stipulated in the documentation. Well-behaved Java libraries are not dependent on the sort order of included jar files. For poorly-behaved Java libraries, ensure that the classpath is in the expected order. If the classpath must be in a non-sorted order, then call #add_to_classpath on each jar file instead.

Parameters:

  • path (String)

    the colon or semi-colon separated directories

Technology idiosyncracy:

  • Java

    The jar files found by this method are added to the classpath



17
18
19
20
21
22
23
24
# File 'lib/jinx/import/class_path_modifier.rb', line 17

def expand_to_class_path(path)
  # the path separator
  sep = path[WINDOWS_PATH_SEP] ? WINDOWS_PATH_SEP : UNIX_PATH_SEP
  # the path directories
  dirs = path.split(sep).map { |dir| File.expand_path(dir) }
  expanded = expand_jars(dirs)
  expanded.each { |dir| add_to_classpath(dir) }
end