Class: RBoss::Datasource

Inherits:
Object
  • Object
show all
Includes:
Component
Defined in:
lib/rboss/components/datasource.rb

Overview

A class to configure a JBoss Datasource.

For converting the property (if it is a Symbol), the above rules are used taking by example a value “:database_url”:

a. The value "database_url"
b. The value "database-url"
c. The value "DatabaseUrl"
d. The value "DATABASE_URL"

The key for finding the correct datasource is the configuration attribute :type, which is used to search in $JBOSS_HOME/docs/examples/jca for the file.

Any key that is not found in the datasource template will be added. If it is a Symbol, the underlines will be converted to hyphens.

For saving the file, the configuration :name will be used in the form “$#name-ds.xml”.

Configuration:

:folder => a folder where this datasource will be saved (default: $JBOSS_HOME/server/$CONFIG/deploy)

if a relative path is given, it will be appended to default value

:encrypt => a flag to indicate if the password should be encrypted (default: false) :type => the type of the datasource :name => a name for saving the file (default: :type) :attributes => a Hash with the attributes that will be changed in template (the only required is :jndi_name)

Any attribute that is not present in datasource xml will be created using this template: <key>value</key>.

author: Marcelo Guimarães <[email protected]>

Direct Known Subclasses

XADatasource

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Component

#initialize, #load_yaml, #new_file_processor

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



63
64
65
# File 'lib/rboss/components/datasource.rb', line 63

def attributes
  @attributes
end

#jndi_nameObject

Returns the value of attribute jndi_name.



63
64
65
# File 'lib/rboss/components/datasource.rb', line 63

def jndi_name
  @jndi_name
end

#nameObject

Returns the value of attribute name.



63
64
65
# File 'lib/rboss/components/datasource.rb', line 63

def name
  @name
end

#typeObject

Returns the value of attribute type.



63
64
65
# File 'lib/rboss/components/datasource.rb', line 63

def type
  @type
end

Instance Method Details

#configure(config) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/rboss/components/datasource.rb', line 73

def configure config
  config[:type] = :mssql if config[:type].to_s == "sqlserver"
  @type = config[:type].to_s.gsub /_/, '-'
  @name = config[:name]
  @name ||= @type.to_s
  @folder = config[:folder].to_s
  @folder = "#{@jboss.profile}/#{@folder}" unless @folder.start_with? '/'
  @attributes = config[:attributes]
  @encrypt = config[:encrypt]
  @jndi_name = @attributes.delete :jndi_name
end

#configure_datasource(xml) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/rboss/components/datasource.rb', line 129

def configure_datasource xml
  if @encrypt
    xml.root.delete_element "//user-name"
    xml.root.delete_element "//password"
    @service = "LocalTxCM"
  end
  @attributes.each do |key, value|
    element = find(xml, key) { |k| "//#{k}" }

    if element
      element.text = value
    else
      insert_attribute xml, key, value
    end
  end
end

#create_login_configObject



156
157
158
# File 'lib/rboss/components/datasource.rb', line 156

def 
  File.open("#{@folder}/#{@name}.login-module.xml", 'w+') { |f| f.write @login_module }
end

#create_login_moduleObject



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rboss/components/datasource.rb', line 160

def 
  @login_module = Document::new "<policy>\n  <application-policy name='\#{@name}'>\n<authentication>\n  <login-module code='org.jboss.resource.security.SecureIdentityLoginModule' flag='required'>\n    <module-option name='username'>\#{@user}</module-option>\n    <module-option name='password'>\#{@password}</module-option>\n    <module-option name='managedConnectionFactoryName'>jboss.jca:name=\#{@jndi_name},service=\#{@service}</module-option>\n  </login-module>\n</authentication>\n  </application-policy>\n</policy>\n"
end

#defaultsObject



65
66
67
68
69
70
71
# File 'lib/rboss/components/datasource.rb', line 65

def defaults
  {
    :folder => "#{@jboss.profile}/deploy",
    :encrypt => false,
    :attributes => {}
  }
end

#find(xml, key) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/rboss/components/datasource.rb', line 116

def find xml, key
  if key.is_a? Symbol
    key = key.to_s
    [key, key.gsub(/_/, '-'), key.camelize, key.upcase].each do |k|
      element = XPath.first xml, yield(k)
      return element if element
    end
  else
    XPath.first xml, yield(key)
  end
  nil
end

#insert_attribute(xml, key, value) ⇒ Object



146
147
148
149
150
151
152
153
154
# File 'lib/rboss/components/datasource.rb', line 146

def insert_attribute xml, key, value
  if key.is_a? Symbol
    element = Element::new key.to_s.gsub /_/, '-'
  else
    element = Element::new key
  end
  element.text = value
  xml.root.elements[1] << element
end

#processObject



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rboss/components/datasource.rb', line 85

def process
  if @encrypt
    @user = @attributes.delete :user
    @user = @attributes.delete :user_name unless @user
    @password = @jboss.encrypt @attributes.delete(:password)
  end
  processor = new_file_processor
  processor.with "#{@jboss.home}/docs/examples/jca/#{@type}-ds.xml", :xml do |action|
    action.to_process do |xml, jboss|
      element = XPath.first xml, "//jndi-name"
      element.text = @jndi_name
      configure_datasource xml

      if @encrypt
        security_domain = Element::new "security-domain"
        security_domain.text = @name

        xml.root.elements[1] << security_domain
      end
      action.copy_to "#{@folder}/#{@name}-ds.xml"
      xml
    end
  end
  @logger.info "Creating datasource #{@name}"
  processor.process
  if @encrypt
    
    
  end
end