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,207 @@
1
+ # = Stop Watch
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: stop_watch.rb 76 2008-10-12 11:45:33Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ require 'commons/lang/state_error'
34
+ require 'commons/lang/time/duration_format_utils'
35
+
36
+ module Commons
37
+ module Lang
38
+ module Time
39
+
40
+ # StopWatch
41
+ #
42
+ # Example:
43
+ # require 'commons/lang/time/stop_watch'
44
+ # # ...
45
+ # stop_watch = Commons::Lang::Time::StopWatch.new
46
+ # stop_watch.reset
47
+ # stop_watch.start
48
+ # # ...
49
+ # stop_watch.stop
50
+ # elapsed_time = stop_watch.get_time # 7 (milliseconds)
51
+ # elapsed_time_str = stop_watch.to_s # 0:00:00.007
52
+ class StopWatch
53
+ # running states
54
+ STATE_UNSTARTED = 0
55
+ STATE_RUNNING = 1
56
+ STATE_STOPPED = 2
57
+ STATE_SUSPENDED = 3
58
+
59
+ # split states
60
+ STATE_UNSPLIT = 10
61
+ STATE_SPLIT = 11
62
+
63
+
64
+ def initialize
65
+ super()
66
+
67
+ # The current running state of the StopWatch.
68
+ @running_state = STATE_UNSTARTED
69
+
70
+ # Whether the stopwatch has a split time recorded.
71
+ @split_state = STATE_UNSPLIT
72
+
73
+ # The start time.
74
+ @start_time = nil
75
+
76
+ # The stop time.
77
+ @stop_time = nil
78
+ end
79
+
80
+
81
+ def start
82
+ if @running_state == STATE_STOPPED
83
+ raise StateError, 'Stopwatch must be reset before being restarted.'
84
+ end
85
+ if @running_state != STATE_UNSTARTED
86
+ raise StateError, 'Stopwatch already started.'
87
+ end
88
+
89
+ @stop_time = nil
90
+ @start_time = ::Time.now
91
+ @running_state = STATE_RUNNING
92
+ end
93
+
94
+
95
+ def stop
96
+ if @running_state != STATE_RUNNING && @running_state != STATE_SUSPENDED
97
+ raise StateError, 'Stopwatch is not running.'
98
+ end
99
+ if @running_state == STATE_RUNNING
100
+ @stop_time = ::Time.now
101
+ end
102
+
103
+ @running_state = STATE_STOPPED
104
+ end
105
+
106
+
107
+ def reset
108
+ @running_state = STATE_UNSTARTED
109
+ @split_state = STATE_UNSPLIT
110
+ @start_time = nil
111
+ @stop_time = nil
112
+ end
113
+
114
+
115
+ def split
116
+ if @running_state != STATE_RUNNING
117
+ raise StateError, 'Stopwatch is not running.'
118
+ end
119
+
120
+ @stop_time = ::Time.now
121
+ @split_state = STATE_SPLIT
122
+ end
123
+
124
+
125
+ def unsplit
126
+ if @split_state != STATE_SPLIT
127
+ raise StateError, 'Stopwatch has not been split.'
128
+ end
129
+
130
+ @stop_time = nil
131
+ @split_state = STATE_UNSPLIT
132
+ end
133
+
134
+
135
+ def suspend
136
+ if @running_state != STATE_RUNNING
137
+ raise StateError, 'Stopwatch must be running to suspend.'
138
+ end
139
+
140
+ @stop_time = ::Time.now
141
+ @running_state = STATE_SUSPENDED
142
+ end
143
+
144
+
145
+ def resume
146
+ if @running_state != STATE_SUSPENDED
147
+ raise StateError, 'Stopwatch must be suspended to resume.'
148
+ end
149
+
150
+ @start_time = ::Time.at(@start_time.to_f + (::Time.now - @stop_time))
151
+ @stop_time = nil
152
+ @running_state = STATE_RUNNING
153
+ end
154
+
155
+
156
+ # Gets the time in milliseconds
157
+ def get_time
158
+ if @running_state == STATE_STOPPED || @running_state == STATE_SUSPENDED
159
+ return sec_to_millis(@stop_time - @start_time)
160
+ elsif @running_state == STATE_UNSTARTED
161
+ return 0
162
+ elsif @running_state == STATE_RUNNING
163
+ return sec_to_millis(::Time.now - @start_time)
164
+ end
165
+
166
+ raise RuntimeError, 'Illegal running state has occured.'
167
+ end
168
+
169
+
170
+ # Gets the split time in milliseconds
171
+ def get_split_time
172
+ if @split_state != STATE_SPLIT
173
+ raise StateError, 'Stopwatch must be split to get the split time.'
174
+ end
175
+
176
+ return sec_to_millis(@stop_time - @start_time)
177
+ end
178
+
179
+
180
+ def sec_to_millis(sec)
181
+ return (sec * 1000).to_i
182
+ end
183
+
184
+
185
+ # Gets the start time (Time object).
186
+ def get_start_time
187
+ if @running_state == STATE_UNSTARTED
188
+ raise StateError, 'Stopwatch has not been started'
189
+ end
190
+
191
+ return @start_time
192
+ end
193
+
194
+
195
+ def to_s
196
+ return DurationFormatUtils.format_duration_hms(get_time)
197
+ end
198
+
199
+
200
+ def to_split_s
201
+ return DurationFormatUtils.format_duration_hms(get_split_time)
202
+ end
203
+ end
204
+
205
+ end
206
+ end
207
+ end
@@ -0,0 +1,159 @@
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: log4r_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/logevent'
34
+
35
+ require 'commons/ruby/exception'
36
+ require 'commons/ruby/log4r'
37
+ require 'commons/logging/log'
38
+
39
+ module Commons
40
+ module Logging
41
+ module Impl
42
+
43
+ class Log4rLogger
44
+ include Commons::Logging::Log
45
+
46
+ FQCN = self.name
47
+
48
+ # The level of the caller that raises a log event.
49
+ # ... (2)target_log_event -> (1)Log#log_method -> (0)Logger#log
50
+ CALLER_LEVEL = 2
51
+
52
+ LOG_PROPAGATED = true
53
+
54
+
55
+ def initialize(param)
56
+ super()
57
+
58
+ case param
59
+ when String # logger name
60
+ @name = param
61
+ @logger = get_logger()
62
+ when Log4r::Logger # logger object, this case for use with a log4r factory.
63
+ if param == nil
64
+ raise ArgumentError,
65
+ 'Warning - nil logger in constructor; possible log4r misconfiguration.'
66
+ end
67
+ @name = param.name
68
+ @logger = param
69
+ else
70
+ raise ArgumentError,
71
+ 'Warning - invalid parameter; logger name or logger object is expected.'
72
+ end
73
+ end
74
+
75
+
76
+ def get_logger
77
+ if @logger == nil
78
+ @logger = Log4r::Logger.get_logger(@name)
79
+ end
80
+
81
+ return @logger
82
+ end
83
+
84
+
85
+ def create_log_event(level, tracer, message)
86
+ logger = get_logger
87
+ tracercall = (logger.trace ? tracer : nil)
88
+ return Log4r::LogEvent.new(level, logger, tracercall, message)
89
+ end
90
+ protected :create_log_event
91
+
92
+
93
+ def log(level, message, exception = nil)
94
+ message = build_message(message, exception)
95
+
96
+ case level
97
+ when TRACE, DEBUG
98
+ get_logger.debug(
99
+ create_log_event(Log4r::DEBUG, caller(CALLER_LEVEL), message),
100
+ LOG_PROPAGATED)
101
+ when INFO
102
+ get_logger.info(
103
+ create_log_event(Log4r::INFO, caller(CALLER_LEVEL), message),
104
+ LOG_PROPAGATED)
105
+ when WARN
106
+ get_logger.warn(
107
+ create_log_event(Log4r::WARN, caller(CALLER_LEVEL), message),
108
+ LOG_PROPAGATED)
109
+ when ERROR
110
+ get_logger.error(
111
+ create_log_event(Log4r::ERROR, caller(CALLER_LEVEL), message),
112
+ LOG_PROPAGATED)
113
+ when FATAL
114
+ get_logger.fatal(
115
+ create_log_event(Log4r::FATAL, caller(CALLER_LEVEL), message),
116
+ LOG_PROPAGATED)
117
+ else
118
+ # do nothing.
119
+ end
120
+ end
121
+
122
+
123
+ def build_message(message, exception)
124
+ if message == nil
125
+ message = '<nil>'
126
+ end
127
+
128
+ if exception != nil
129
+ return message.to_s \
130
+ + ' <' + exception.class.name + ': ' + exception.message.to_s + '; ' \
131
+ + exception.stack_trace_string + '>';
132
+ else
133
+ return message.to_s
134
+ end
135
+ end
136
+ protected :build_message
137
+
138
+
139
+ def enabled_for?(level)
140
+ case level
141
+ when TRACE, DEBUG
142
+ return get_logger.debug?
143
+ when INFO
144
+ return get_logger.info?
145
+ when WARN
146
+ return get_logger.warn?
147
+ when ERROR
148
+ return get_logger.error?
149
+ when FATAL
150
+ return get_logger.fatal?
151
+ else
152
+ return false
153
+ end
154
+ end
155
+ end
156
+
157
+ end
158
+ end
159
+ end
@@ -0,0 +1,253 @@
1
+ # = Log Factory Default Implementation
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_impl.rb 77 2008-10-12 12:07:49Z whitestar $
29
+ #
30
+ # == Since
31
+ # File available since Release 0.8.0
32
+
33
+ require 'commons'
34
+ require 'commons/io/filename_utils'
35
+ require 'commons/lang/class_loader'
36
+ require 'commons/logging/log'
37
+ require 'commons/logging/log_configuration_error'
38
+ require 'commons/logging/log_factory'
39
+
40
+ module Commons
41
+ module Logging
42
+ module Impl
43
+
44
+ class LogFactoryImpl < Commons::Logging::LogFactory
45
+ # The property name used to identify the LOG_MODULE inclusion class name.
46
+ LOG_PROPERTY = 'commons.logging.Log'
47
+
48
+ # The fully qualified class name of the log module.
49
+ LOG_MODULE = 'Commons::Logging::Log'
50
+
51
+ # The log factory setter name.
52
+ LOG_METHOD_NAME = 'set_log_factory'
53
+
54
+ LOG4R_LOGGER_CLASS = 'Commons::Logging::Impl::Log4rLogger'
55
+
56
+ SIMPLELOG_LOGGER_CLASS = 'Commons::Logging::Impl::SimpleLog'
57
+
58
+
59
+ attr_accessor :attributes, :instances, :log_class, :log_method
60
+
61
+ protected :attributes, :instances, :log_class, :log_method
62
+
63
+
64
+ def initialize
65
+ super()
66
+
67
+ @attributes = Hash.new
68
+
69
+ # Already instantiated logger objects. keyed by name.
70
+ @instances = Hash.new
71
+
72
+ # The fully qualified class name of LOG_MODULE inclusion.
73
+ @log_class_name = nil
74
+
75
+ # The inclusion class object of the LOG_MODULE
76
+ @log_class = nil
77
+
78
+ # The one-argument LOG_METHOD_NAME method of the selected
79
+ # LOG_MODULE inclusion class, if it exists.
80
+ @log_method = nil
81
+ end
82
+
83
+
84
+ def get_attribute(name)
85
+ if @attributes.has_key?(name)
86
+ return @attributes[name]
87
+ else
88
+ return nil
89
+ end
90
+ end
91
+
92
+
93
+ def get_attribute_names
94
+ return @attributes.keys
95
+ end
96
+
97
+
98
+ def get_instance(name)
99
+ unless @instances.has_key?(name)
100
+ @instances[name] = new_instance(name)
101
+ end
102
+
103
+ return @instances[name]
104
+ end
105
+
106
+
107
+ def release
108
+ @instances.clear
109
+ end
110
+
111
+
112
+ def remove_attribute(name)
113
+ @attributes.delete(name)
114
+ end
115
+
116
+
117
+ def set_attribute(name, value)
118
+ if value == nil
119
+ @attributes.delete(name)
120
+ else
121
+ @attributes[name] = value
122
+ end
123
+ end
124
+
125
+
126
+ # Gets Log module inclusion class name.
127
+ # throws Commons::Logging::LogConfigurationError
128
+ def get_log_class_name
129
+ unless @log_class_name == nil
130
+ return @log_class_name
131
+ end
132
+
133
+ # 1. Explicitly defined
134
+ @log_class_name = get_attribute(LOG_PROPERTY)
135
+
136
+ # 2. Environmental variable
137
+ if @log_class_name == nil
138
+ env = ENV[LOG_PROPERTY]
139
+ unless env == nil
140
+ @log_class_name = env
141
+ end
142
+ end
143
+ # 3. Log4r
144
+ if @log_class_name == nil && log4r_available?
145
+ @log_class_name = LOG4R_LOGGER_CLASS
146
+ end
147
+ # 4. SimpleLog
148
+ if @log_class_name == nil
149
+ @log_class_name = SIMPLELOG_LOGGER_CLASS
150
+ end
151
+
152
+ init_log_configuration(@log_class_name)
153
+
154
+ return @log_class_name
155
+ end
156
+ protected :get_log_class_name
157
+
158
+
159
+ # Initializes logging framework configuration.
160
+ def init_log_configuration(log_class_name)
161
+ if log_class_name == LOG4R_LOGGER_CLASS
162
+ log4r_conf = get_attribute(Commons::LOG4R_CONF_PROPERTY)
163
+ unless log4r_conf == nil
164
+ unless Commons::IO::FilenameUtils.absolute?(log4r_conf)
165
+ log4r_conf = Commons::Lang::ClassLoader.get_resource(log4r_conf)
166
+ end
167
+ unless log4r_conf == nil
168
+ Commons::ENV[Commons::LOG4R_CONF_PROPERTY] = log4r_conf
169
+ else
170
+ raise Commons::Logging::LogConfigurationError,
171
+ 'Log4r configuration file not found, property key: ' \
172
+ + Commons::LOG4R_CONF_PROPERTY
173
+ end
174
+ end
175
+ end
176
+ end
177
+ protected :init_log_configuration
178
+
179
+
180
+ # Obtains LOG_MODULE inclusion's class.
181
+ # throws Commons::Logging::LogConfigurationError
182
+ def get_log_class
183
+ unless @log_class == nil
184
+ return @log_class
185
+ end
186
+
187
+ begin
188
+ log_class_name = get_log_class_name
189
+
190
+ Commons::Lang::ClassLoader.load(log_class_name)
191
+ @log_class = eval(log_class_name)
192
+ if @log_class == nil
193
+ raise Commons::Logging::LogConfigurationError,
194
+ 'No suitable Log implementation for ' + log_class_name
195
+ end
196
+ unless @log_class.include?(eval(LOG_MODULE))
197
+ raise Commons::Logging::LogConfigurationError(
198
+ 'Class ' . log_class_name + ' does not include \'' \
199
+ + LOG_MODULE + '\'.')
200
+ end
201
+ rescue Commons::Logging::LogConfigurationError
202
+ raise
203
+ rescue Exception => e
204
+ raise Commons::Logging::LogConfigurationError, e.message
205
+ end
206
+
207
+ begin
208
+ @log_method = @log_class.instance_method(LOG_METHOD_NAME)
209
+ rescue Exception => e
210
+ @log_method = nil
211
+ end
212
+
213
+ return @log_class;
214
+ end
215
+ protected :get_log_class
216
+
217
+
218
+ # Tests whether Log4R is available or not.
219
+ def log4r_available?
220
+ # check the resource existence.
221
+ begin
222
+ require 'log4r'
223
+ return true
224
+ rescue LoadError
225
+ return false
226
+ end
227
+ end
228
+ protected :log4r_available?
229
+
230
+
231
+ # Creates and returns LOG_MODULE instance
232
+ # throws Commons::Logging::LogConfigurationError
233
+ def new_instance(name)
234
+ instance = nil
235
+ begin
236
+ # Commons::Logging::Log implementation must have one-argument constructor.
237
+ instance = get_log_class.new(name)
238
+ unless @log_method == nil
239
+ @log_method.bind(instance).call(self)
240
+ end
241
+ return instance
242
+ rescue Commons::Logging::LogConfigurationError
243
+ raise
244
+ rescue Exception => e
245
+ raise Commons::Logging::LogConfigurationError, e.message
246
+ end
247
+ end
248
+ protected :new_instance
249
+ end
250
+
251
+ end
252
+ end
253
+ end