rcommons 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. data/COPYING +202 -0
  2. data/Changes.rdoc +4 -0
  3. data/README.rdoc +21 -0
  4. data/Rakefile +277 -0
  5. data/doc/jamis.rb +589 -0
  6. data/lib/commons.rb +44 -0
  7. data/lib/commons/io/filename_utils.rb +54 -0
  8. data/lib/commons/lang/builder/to_s_builder.rb +124 -0
  9. data/lib/commons/lang/builder/to_s_style.rb +493 -0
  10. data/lib/commons/lang/class_loader.rb +77 -0
  11. data/lib/commons/lang/class_utils.rb +50 -0
  12. data/lib/commons/lang/object_utils.rb +61 -0
  13. data/lib/commons/lang/state_error.rb +41 -0
  14. data/lib/commons/lang/system_utils.rb +113 -0
  15. data/lib/commons/lang/time/date_utils.rb +47 -0
  16. data/lib/commons/lang/time/duration_format_utils.rb +279 -0
  17. data/lib/commons/lang/time/stop_watch.rb +207 -0
  18. data/lib/commons/logging/impl/log4r_logger.rb +159 -0
  19. data/lib/commons/logging/impl/log_factory_impl.rb +253 -0
  20. data/lib/commons/logging/impl/no_op_log.rb +59 -0
  21. data/lib/commons/logging/impl/simple_log.rb +251 -0
  22. data/lib/commons/logging/log.rb +106 -0
  23. data/lib/commons/logging/log_configuration_error.rb +40 -0
  24. data/lib/commons/logging/log_factory.rb +177 -0
  25. data/lib/commons/ruby/exception.rb +46 -0
  26. data/lib/commons/ruby/log4r.rb +34 -0
  27. data/lib/commons/ruby/log4r/logger.rb +116 -0
  28. data/lib/commons/ruby/string.rb +55 -0
  29. data/lib/commons/ruby/test/unit.rb +33 -0
  30. data/lib/commons/ruby/test/unit/assertions.rb +50 -0
  31. data/lib/commons/util/properties.rb +84 -0
  32. data/test/commons/io/filename_utils_test.rb +69 -0
  33. data/test/commons/lang/builder/to_s_builder_test.rb +101 -0
  34. data/test/commons/lang/system_utils_test.rb +75 -0
  35. data/test/commons/lang/time/duration_format_utils_test.rb +261 -0
  36. data/test/commons/lang/time/stop_watch_test.rb +232 -0
  37. data/test/commons/logging/impl/log4r_logger_test.rb +133 -0
  38. data/test/commons/logging/log_factory_test.rb +80 -0
  39. data/test/commons/ruby/string_test.rb +64 -0
  40. data/test/commons/util/properties_test.rb +92 -0
  41. data/test/log4r/logger_test.rb +92 -0
  42. metadata +123 -0
@@ -0,0 +1,177 @@
1
+ # = Log Factory
2
+ #
3
+ #--
4
+ # Ruby version 1.8
5
+ #
6
+ # == Authors
7
+ # * Yomei Komiya
8
+ #
9
+ # == Copyright
10
+ # 2008 the original author or authors.
11
+ #
12
+ # == License
13
+ # Apache License 2.0
14
+ #
15
+ # Licensed under the Apache License, Version 2.0 (the "License");
16
+ # you may not use this file except in compliance with the License.
17
+ # You may obtain a copy of the License at
18
+ #
19
+ # http://www.apache.org/licenses/LICENSE-2.0
20
+ #
21
+ # Unless required by applicable law or agreed to in writing, software
22
+ # distributed under the License is distributed on an "AS IS" BASIS,
23
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ # See the License for the specific language governing permissions and
25
+ # limitations under the License.
26
+ #++
27
+ # == Version
28
+ # SVN: $Id: log_factory.rb 79 2008-10-16 13:51:47Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ require 'commons/lang/class_loader'
34
+ require 'commons/logging/log_configuration_error'
35
+ require 'commons/util/properties'
36
+
37
+ module Commons
38
+ module Logging
39
+
40
+ # Abstract base log factory class.
41
+ #
42
+ # Example:
43
+ # require 'commons/ruby/log4r'
44
+ #
45
+ # module Example
46
+ # class Sample
47
+ # LOG = Log4r::Logger.get_logger(self.name)
48
+ # #@@log = Log4r::Logger.get_logger(self.name) # for Ruby 1.9 or later.
49
+ # # ...
50
+ # if LOG.debug?
51
+ # LOG.debug('This is DEBUG log.')
52
+ # end
53
+ # LOG.info('This is INFO log.')
54
+ # # ...
55
+ # end
56
+ # end
57
+ class LogFactory
58
+ # Commons.Logging.LogFactory property name.
59
+ FACTORY_PROPERTY = 'commons.logging.LogFactory'
60
+
61
+ # Default implementation class name of Commons.Logging.LogFactory.
62
+ FACTORY_DEFAULT = 'Commons::Logging::Impl::LogFactoryImpl'
63
+
64
+ # Default properties file (relative path on $LOAD_PATH)
65
+ FACTORY_PROPERTIES = 'commons-logging.properties'
66
+
67
+
68
+ def initialize
69
+ # do nothing.
70
+ end
71
+
72
+
73
+ # Cached factories.
74
+ @@factories = Hash.new
75
+
76
+
77
+ # Gets Commons.Logging.LogFactory instance.
78
+ #
79
+ # throws Commons::Logging::LogConfigurationError
80
+ # if the implementation class is not available or cannot be instantiated.
81
+ def self.get_factory
82
+ factory = nil
83
+
84
+ factory = get_cached_factory
85
+ unless factory == nil
86
+ return factory
87
+ end
88
+
89
+ props = nil
90
+ props_path = Commons::Lang::ClassLoader.get_resource(FACTORY_PROPERTIES)
91
+ File.open(props_path, 'r') {|file|
92
+ props = Commons::Util::Properties.new
93
+ props.load(file)
94
+ }
95
+
96
+ # 1st, try the system property.
97
+ factory_class = ENV[FACTORY_PROPERTY]
98
+ unless factory_class == nil
99
+ factory = new_factory(factory_class)
100
+ end
101
+
102
+ # 2nd try a properties file.
103
+ if factory == nil && props != nil
104
+ factory_class = props.get_property(FACTORY_PROPERTY)
105
+ unless factory_class == nil
106
+ factory = new_factory(factory_class)
107
+ end
108
+ end
109
+
110
+ # 3rd, try the fallback implementation class
111
+ if factory == nil
112
+ factory = new_factory(FACTORY_DEFAULT)
113
+ end
114
+
115
+ # cache the factory
116
+ if factory != nil
117
+ cache_factory(factory)
118
+ if props != nil
119
+ props.property_names.each {|name|
120
+ factory.set_attribute(name, props.get_property(name))
121
+ }
122
+ end
123
+ end
124
+
125
+ return factory
126
+ end
127
+
128
+
129
+ def self.get_log(name)
130
+ return get_factory.get_instance(name)
131
+ end
132
+
133
+
134
+ # Obtains cached factory.
135
+ def self.get_cached_factory
136
+ if @@factories.has_key?('defaultKey')
137
+ return @@factories['defaultKey']
138
+ else
139
+ return nil
140
+ end
141
+ end
142
+ class << self
143
+ private :get_cached_factory
144
+ end
145
+
146
+
147
+ # Caches log factory instance.
148
+ def self.cache_factory(factory)
149
+ unless factory == nil
150
+ @@factories['defaultKey'] = factory
151
+ end
152
+ end
153
+ class << self
154
+ private :cache_factory
155
+ end
156
+
157
+
158
+ # Creates and returns new Commons_Logging_LogFactory instance.
159
+ #
160
+ # throws Commons::Logging::LogConfigurationError
161
+ def self.new_factory(factory_class)
162
+ begin
163
+ Commons::Lang::ClassLoader::load(factory_class)
164
+ return eval(factory_class).new
165
+ rescue LoadError => le
166
+ raise LogConfigurationError, le.message
167
+ rescue Exception => e
168
+ raise LogConfigurationError, e.message
169
+ end
170
+ end
171
+ class << self
172
+ protected :new_factory
173
+ end
174
+ end
175
+
176
+ end
177
+ end
@@ -0,0 +1,46 @@
1
+ # = Exception
2
+ #
3
+ #--
4
+ # Ruby version 1.8
5
+ #
6
+ # == Authors
7
+ # * Yomei Komiya
8
+ #
9
+ # == Copyright
10
+ # 2008 the original author or authors.
11
+ #
12
+ # == License
13
+ # Apache License 2.0
14
+ #
15
+ # Licensed under the Apache License, Version 2.0 (the "License");
16
+ # you may not use this file except in compliance with the License.
17
+ # You may obtain a copy of the License at
18
+ #
19
+ # http://www.apache.org/licenses/LICENSE-2.0
20
+ #
21
+ # Unless required by applicable law or agreed to in writing, software
22
+ # distributed under the License is distributed on an "AS IS" BASIS,
23
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ # See the License for the specific language governing permissions and
25
+ # limitations under the License.
26
+ #++
27
+ # == Version
28
+ # SVN: $Id: exception.rb 77 2008-10-12 12:07:49Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ require 'commons/lang/system_utils'
34
+
35
+ class Exception
36
+ def stack_trace_string
37
+ trace = self.backtrace
38
+ if trace == nil
39
+ return 'no backtrace'
40
+ elsif trace.kind_of?(String)
41
+ return trace
42
+ else
43
+ trace.join(Commons::Lang::SystemUtils.line_separator)
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,34 @@
1
+ # = Log4r
2
+ #
3
+ #--
4
+ # Ruby version 1.8
5
+ #
6
+ # == Authors
7
+ # * Yomei Komiya
8
+ #
9
+ # == Copyright
10
+ # 2008 the original author or authors.
11
+ #
12
+ # == License
13
+ # Apache License 2.0
14
+ #
15
+ # Licensed under the Apache License, Version 2.0 (the "License");
16
+ # you may not use this file except in compliance with the License.
17
+ # You may obtain a copy of the License at
18
+ #
19
+ # http://www.apache.org/licenses/LICENSE-2.0
20
+ #
21
+ # Unless required by applicable law or agreed to in writing, software
22
+ # distributed under the License is distributed on an "AS IS" BASIS,
23
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ # See the License for the specific language governing permissions and
25
+ # limitations under the License.
26
+ #++
27
+ # == Version
28
+ # SVN: $Id: log4r.rb 77 2008-10-12 12:07:49Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ require 'log4r'
34
+ require 'commons/ruby/log4r/logger'
@@ -0,0 +1,116 @@
1
+ # = Log4r::Logger
2
+ #
3
+ #--
4
+ # Ruby version 1.8
5
+ #
6
+ # == Authors
7
+ # * Yomei Komiya
8
+ #
9
+ # == Copyright
10
+ # 2008 the original author or authors.
11
+ #
12
+ # == License
13
+ # Apache License 2.0
14
+ #
15
+ # Licensed under the Apache License, Version 2.0 (the "License");
16
+ # you may not use this file except in compliance with the License.
17
+ # You may obtain a copy of the License at
18
+ #
19
+ # http://www.apache.org/licenses/LICENSE-2.0
20
+ #
21
+ # Unless required by applicable law or agreed to in writing, software
22
+ # distributed under the License is distributed on an "AS IS" BASIS,
23
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ # See the License for the specific language governing permissions and
25
+ # limitations under the License.
26
+ #++
27
+ # == Version
28
+ # SVN: $Id: logger.rb 77 2008-10-12 12:07:49Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ require 'log4r'
34
+
35
+ require 'commons'
36
+ require 'commons/lang/class_loader'
37
+
38
+ module Log4r
39
+
40
+ # This is the extended Log4r::Logger class of rCommons.
41
+ #
42
+ # Example:
43
+ # require 'commons/ruby/log4r'
44
+ #
45
+ # module Example
46
+ # class Sample
47
+ # LOG = Log4r::Logger.get_logger(self.name)
48
+ # #@@log = Log4r::Logger.get_logger(self.name) # for Ruby 1.9 or later.
49
+ # # ...
50
+ # if LOG.debug?
51
+ # LOG.debug('This is DEBUG log.')
52
+ # end
53
+ # LOG.info('This is INFO log.')
54
+ # # ...
55
+ # end
56
+ # end
57
+ class Logger
58
+ DEFAULT_XML_CONFIGURATION_FILE = 'log4r.xml'
59
+
60
+ DEFAULT_YAML_CONFIGURATION_FILE = 'log4r.yaml'
61
+
62
+ @@current_configuration_file = nil
63
+
64
+
65
+ def self.static_initialize
66
+ # 1. ENV
67
+ config = ENV[Commons::LOG4R_CONF_PROPERTY]
68
+ # 2. Commons::ENV
69
+ if config == nil
70
+ config = Commons::ENV[Commons::LOG4R_CONF_PROPERTY]
71
+ end
72
+ # 3. Default XML configuration file
73
+ if config == nil
74
+ config = Commons::Lang::ClassLoader.get_resource(DEFAULT_XML_CONFIGURATION_FILE)
75
+ end
76
+ # 4. Default YAML configuration file
77
+ if config == nil
78
+ config = Commons::Lang::ClassLoader.get_resource(DEFAULT_YAML_CONFIGURATION_FILE)
79
+ end
80
+
81
+ if config != nil
82
+ case config[config.rindex('.') .. -1]
83
+ when '.xml'
84
+ require 'log4r/configurator'
85
+ Configurator.load_xml_file(config)
86
+ @@current_configuration_file = config
87
+ when '.yaml', '.yml'
88
+ require 'log4r/yamlconfigurator'
89
+ YamlConfigurator.load_yaml_file(config)
90
+ @@current_configuration_file = config
91
+ else
92
+ # do nothing.
93
+ end
94
+ end
95
+ end
96
+ class << self
97
+ protected :static_initialize
98
+ end
99
+ self.static_initialize
100
+
101
+
102
+ def self.get_logger(name)
103
+ if Logger[name] == nil
104
+ logger = Logger.new(name)
105
+ if logger.additive
106
+ logger.trace = logger.parent.trace # inherit trace flag
107
+ end
108
+ return logger
109
+ else
110
+ return Logger[name]
111
+ end
112
+ end
113
+
114
+ end
115
+
116
+ end
@@ -0,0 +1,55 @@
1
+ # = String
2
+ #
3
+ #--
4
+ # Ruby version 1.8
5
+ #
6
+ # == Authors
7
+ # * Yomei Komiya
8
+ #
9
+ # == Copyright
10
+ # 2008 the original author or authors.
11
+ #
12
+ # == License
13
+ # Apache License 2.0
14
+ #
15
+ # Licensed under the Apache License, Version 2.0 (the "License");
16
+ # you may not use this file except in compliance with the License.
17
+ # You may obtain a copy of the License at
18
+ #
19
+ # http://www.apache.org/licenses/LICENSE-2.0
20
+ #
21
+ # Unless required by applicable law or agreed to in writing, software
22
+ # distributed under the License is distributed on an "AS IS" BASIS,
23
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ # See the License for the specific language governing permissions and
25
+ # limitations under the License.
26
+ #++
27
+ # == Version
28
+ # SVN: $Id: string.rb 77 2008-10-12 12:07:49Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ require 'facets/string'
34
+
35
+ class String
36
+ # java.lang.String#substring (Java-like signature)
37
+ def substring(begin_index, end_index = nil)
38
+ if end_index == nil
39
+ return self[begin_index .. -1]
40
+ else
41
+ return self[begin_index ... end_index]
42
+ end
43
+ end
44
+
45
+
46
+ # java.lang.String#replaceAll (Java-like signature)
47
+ def replace_all(pattern, replace)
48
+ result = self.gsub(pattern, replace)
49
+ if result == nil
50
+ return self
51
+ else
52
+ return result
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,33 @@
1
+ # = Test::Unit
2
+ #
3
+ #--
4
+ # Ruby version 1.8
5
+ #
6
+ # == Authors
7
+ # * Yomei Komiya
8
+ #
9
+ # == Copyright
10
+ # 2008 the original author or authors.
11
+ #
12
+ # == License
13
+ # Apache License 2.0
14
+ #
15
+ # Licensed under the Apache License, Version 2.0 (the "License");
16
+ # you may not use this file except in compliance with the License.
17
+ # You may obtain a copy of the License at
18
+ #
19
+ # http://www.apache.org/licenses/LICENSE-2.0
20
+ #
21
+ # Unless required by applicable law or agreed to in writing, software
22
+ # distributed under the License is distributed on an "AS IS" BASIS,
23
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
+ # See the License for the specific language governing permissions and
25
+ # limitations under the License.
26
+ #++
27
+ # == Version
28
+ # SVN: $Id: unit.rb 77 2008-10-12 12:07:49Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ require 'commons/ruby/test/unit/assertions'