Class: Mondrian::OLAP::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/mondrian/olap/connection.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params = {}) ⇒ Connection

Returns a new instance of Connection.



13
14
15
16
17
18
# File 'lib/mondrian/olap/connection.rb', line 13

def initialize(params={})
  @params = params
  @driver = params[:driver]
  @connected = false
  @raw_connection = nil
end

Instance Attribute Details

#raw_cache_controlObject (readonly)

Returns the value of attribute raw_cache_control.



10
11
12
# File 'lib/mondrian/olap/connection.rb', line 10

def raw_cache_control
  @raw_cache_control
end

#raw_catalogObject (readonly)

Returns the value of attribute raw_catalog.



10
11
12
# File 'lib/mondrian/olap/connection.rb', line 10

def raw_catalog
  @raw_catalog
end

#raw_connectionObject (readonly)

Returns the value of attribute raw_connection.



10
11
12
# File 'lib/mondrian/olap/connection.rb', line 10

def raw_connection
  @raw_connection
end

#raw_schemaObject (readonly)

Returns the value of attribute raw_schema.



10
11
12
# File 'lib/mondrian/olap/connection.rb', line 10

def raw_schema
  @raw_schema
end

#raw_schema_readerObject (readonly)

Returns the value of attribute raw_schema_reader.



10
11
12
# File 'lib/mondrian/olap/connection.rb', line 10

def raw_schema_reader
  @raw_schema_reader
end

Class Method Details

.create(params) ⇒ Object



4
5
6
7
8
# File 'lib/mondrian/olap/connection.rb', line 4

def self.create(params)
  connection = new(params)
  connection.connect
  connection
end

.shutdown_static_mondrian_server!Object

Force shutdown of static MondrianServer, should not normally be used. Can be used in at_exit block if JRuby based plugin is unloaded from other Java application. WARNING: Mondrian will be unusable after calling this method!



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'lib/mondrian/olap/connection.rb', line 167

def self.shutdown_static_mondrian_server!
  static_mondrian_server = Java::MondrianOlap::MondrianServer.forId(nil)

  # force Mondrian to think that static_mondrian_server is not static MondrianServer
  mondrian_server_registry = Java::MondrianServer::MondrianServerRegistry::INSTANCE
  f = mondrian_server_registry.java_class.declared_field("staticServer")
  f.accessible = true
  f.set_value(mondrian_server_registry, nil)

  static_mondrian_server.shutdown

  # shut down expiring reference timer thread
  f = Java::MondrianUtil::ExpiringReference.java_class.declared_field("timer")
  f.accessible = true
  expiring_reference_timer = f.static_value.to_java
  expiring_reference_timer.cancel

  # shut down Mondrian Monitor
  cons = Java::MondrianServer.__send__(:"MonitorImpl$ShutdownCommand").java_class.declared_constructor
  cons.accessible = true
  shutdown_command = cons.new_instance.to_java

  cons = Java::MondrianServer.__send__(:"MonitorImpl$Handler").java_class.declared_constructor
  cons.accessible = true
  handler = cons.new_instance.to_java

  pair = Java::mondrian.util.Pair.new handler, shutdown_command

  f = Java::MondrianServer::MonitorImpl.java_class.declared_field("ACTOR")
  f.accessible = true
  monitor_actor = f.static_value.to_java

  f = monitor_actor.java_class.declared_field("eventQueue")
  f.accessible = true
  event_queue = f.value(monitor_actor)

  event_queue.put pair

  # shut down connection pool thread
  f = Java::mondrian.rolap.RolapConnectionPool.java_class.declared_field("instance")
  f.accessible = true
  rolap_connection_pool = f.static_value.to_java
  f = rolap_connection_pool.java_class.declared_field("mapConnectKeyToPool")
  f.accessible = true
  map_connect_key_to_pool = f.value(rolap_connection_pool)
  map_connect_key_to_pool.values.each do |pool|
    pool.close if pool && !pool.isClosed
  end

  # unregister MBean
  mbs = Java::JavaLangManagement::ManagementFactory.getPlatformMBeanServer
  mbean_name = Java::JavaxManagement::ObjectName.new("mondrian.server:type=Server-#{static_mondrian_server.getId}")
  begin
    mbs.unregisterMBean(mbean_name)
  rescue Java::JavaxManagement::InstanceNotFoundException
  end

  true
end

Instance Method Details

#available_role_namesObject



118
119
120
# File 'lib/mondrian/olap/connection.rb', line 118

def available_role_names
  @raw_connection.getAvailableRoleNames.to_a
end

#closeObject



69
70
71
72
73
74
# File 'lib/mondrian/olap/connection.rb', line 69

def close
  @raw_connection.close
  @connected = false
  @raw_connection = @raw_jdbc_connection = nil
  true
end

#connectObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/mondrian/olap/connection.rb', line 20

def connect
  Error.wrap_native_exception do
    # hack to call private constructor of MondrianOlap4jDriver
    # to avoid using DriverManager which fails to load JDBC drivers
    # because of not seeing JRuby required jar files
    cons = Java::MondrianOlap4j::MondrianOlap4jDriver.java_class.declared_constructor
    cons.accessible = true
    driver = cons.new_instance.to_java

    props = java.util.Properties.new
    props.setProperty('JdbcUser', @params[:username]) if @params[:username]
    props.setProperty('JdbcPassword', @params[:password]) if @params[:password]

    # on Oracle increase default row prefetch size
    # as default 10 is very low and slows down loading of all dimension members
    if @driver == 'oracle'
      prefetch_rows = @params[:prefetch_rows] || 100
      props.setProperty("jdbc.defaultRowPrefetch", prefetch_rows.to_s)
    end

    conn_string = connection_string

    # latest Mondrian version added ClassResolver which uses current thread class loader to load some classes
    # therefore need to set it to JRuby class loader to ensure that Mondrian classes are found
    # (e.g. when running mondrian-olap inside OSGi container)
    current_thread = Java::JavaLang::Thread.currentThread
    class_loader = current_thread.getContextClassLoader
    begin
      current_thread.setContextClassLoader JRuby.runtime.jruby_class_loader
      @raw_jdbc_connection = driver.connect(conn_string, props)
    ensure
      current_thread.setContextClassLoader(class_loader)
    end

    @raw_connection = @raw_jdbc_connection.unwrap(Java::OrgOlap4j::OlapConnection.java_class)
    @raw_catalog = @raw_connection.getOlapCatalog
    # currently it is assumed that there is just one schema per connection catalog
    @raw_schema = @raw_catalog.getSchemas.first
    @raw_schema_reader = @raw_connection.getMondrianConnection.getSchemaReader
    @raw_cache_control = @raw_connection.getMondrianConnection.getCacheControl(nil)
    @connected = true
    true
  end
end

#connected?Boolean

Returns:

  • (Boolean)


65
66
67
# File 'lib/mondrian/olap/connection.rb', line 65

def connected?
  @connected
end

#cube(name) ⇒ Object



106
107
108
# File 'lib/mondrian/olap/connection.rb', line 106

def cube(name)
  Cube.get(self, name)
end

#cube_namesObject



102
103
104
# File 'lib/mondrian/olap/connection.rb', line 102

def cube_names
  @raw_schema.getCubes.map{|c| c.getName}
end

#execute(query_string, parameters = {}) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/mondrian/olap/connection.rb', line 76

def execute(query_string, parameters = {})
  Error.wrap_native_exception do
    statement = @raw_connection.prepareOlapStatement(query_string)
    set_statement_parameters(statement, parameters)
    Result.new(self, statement.executeQuery())
  end
end

#execute_drill_through(query_string) ⇒ Object



91
92
93
94
95
96
# File 'lib/mondrian/olap/connection.rb', line 91

def execute_drill_through(query_string)
  Error.wrap_native_exception do
    statement = @raw_connection.createStatement
    Result::DrillThrough.new(statement.executeQuery(query_string))
  end
end

#flush_schema_cacheObject

Will affect only the next created connection. If it is necessary to clear all schema cache then flush_schema_cache should be called, then close and then new connection should be created.



112
113
114
115
116
# File 'lib/mondrian/olap/connection.rb', line 112

def flush_schema_cache
  unwrapped_connection = @raw_connection.unwrap(Java::MondrianOlap::Connection.java_class)
  raw_cache_control = unwrapped_connection.getCacheControl(nil)
  raw_cache_control.flushSchemaCache
end

#from(cube_name) ⇒ Object



98
99
100
# File 'lib/mondrian/olap/connection.rb', line 98

def from(cube_name)
  Query.from(self, cube_name)
end

#localeObject



146
147
148
# File 'lib/mondrian/olap/connection.rb', line 146

def locale
  @raw_connection.getLocale.toString
end

#locale=(locale) ⇒ Object

Raises:

  • (ArgumentError)


150
151
152
153
154
155
# File 'lib/mondrian/olap/connection.rb', line 150

def locale=(locale)
  locale_elements = locale.to_s.split('_')
  raise ArgumentError, "invalid locale string #{locale.inspect}" unless [1,2,3].include?(locale_elements.length)
  java_locale = Java::JavaUtil::Locale.new(*locale_elements)
  @raw_connection.setLocale(java_locale)
end

#mondrian_parameter(parameter_name) ⇒ Object

access mondrian.olap.Parameter object



85
86
87
88
89
# File 'lib/mondrian/olap/connection.rb', line 85

def mondrian_parameter(parameter_name)
  Error.wrap_native_exception do
    @raw_schema_reader.getParameter(parameter_name)
  end
end

#mondrian_serverObject

access MondrianServer instance



158
159
160
161
162
# File 'lib/mondrian/olap/connection.rb', line 158

def mondrian_server
  Error.wrap_native_exception do
    @raw_connection.getMondrianConnection.getServer
  end
end

#role_nameObject



122
123
124
# File 'lib/mondrian/olap/connection.rb', line 122

def role_name
  @raw_connection.getRoleName
end

#role_name=(name) ⇒ Object



132
133
134
135
136
# File 'lib/mondrian/olap/connection.rb', line 132

def role_name=(name)
  Error.wrap_native_exception do
    @raw_connection.setRoleName(name)
  end
end

#role_namesObject



126
127
128
129
130
# File 'lib/mondrian/olap/connection.rb', line 126

def role_names
  # workaround to access non-public method (was not public when using inside Torquebox)
  # @raw_connection.getRoleNames.to_a
  @raw_connection.java_method(:getRoleNames).call.to_a
end

#role_names=(names) ⇒ Object



138
139
140
141
142
143
144
# File 'lib/mondrian/olap/connection.rb', line 138

def role_names=(names)
  Error.wrap_native_exception do
    # workaround to access non-public method (was not public when using inside Torquebox)
    # @raw_connection.setRoleNames(Array(names))
    @raw_connection.java_method(:setRoleNames, [Java::JavaUtil::List.java_class]).call(Array(names))
  end
end