|
| 1 | +# Cloud Foundry Java Buildpack |
| 2 | +# Copyright 2013-2017 the original author or authors. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +require 'java_buildpack/component/versioned_dependency_component' |
| 17 | +require 'java_buildpack/container' |
| 18 | +require 'java_buildpack/container/tomcat/tomcat_utils' |
| 19 | +require 'java_buildpack/logging/logger_factory' |
| 20 | + |
| 21 | +module JavaBuildpack |
| 22 | + module Container |
| 23 | + |
| 24 | + # Encapsulates the detect, compile, and release functionality for Tomcat Redis support. |
| 25 | + class TomcatGeodeStore < JavaBuildpack::Component::VersionedDependencyComponent |
| 26 | + include JavaBuildpack::Container |
| 27 | + |
| 28 | + # (see JavaBuildpack::Component::BaseComponent#compile) |
| 29 | + def compile |
| 30 | + return unless supports? |
| 31 | + download_tar(false, tomcat_lib, tar_name) |
| 32 | + mutate_context |
| 33 | + mutate_server |
| 34 | + create_cache_client_xml |
| 35 | + end |
| 36 | + |
| 37 | + # (see JavaBuildpack::Component::BaseComponent#release) |
| 38 | + def release |
| 39 | + return unless supports? |
| 40 | + credentials = @application.services.find_service(FILTER)['credentials'] |
| 41 | + user = credentials[KEY_USERS].find { |u| u['username'] == 'cluster_operator' } |
| 42 | + |
| 43 | + @droplet.java_opts.add_system_property 'gemfire.security-username', 'cluster_operator' |
| 44 | + @droplet.java_opts.add_system_property 'gemfire.security-password', user['password'] |
| 45 | + @droplet.java_opts.add_system_property 'gemfire.security-client-auth-init', |
| 46 | + 'io.pivotal.cloudcache.ClientAuthInitialize.create' |
| 47 | + end |
| 48 | + |
| 49 | + protected |
| 50 | + |
| 51 | + # (see JavaBuildpack::Component::VersionedDependencyComponent#supports?) |
| 52 | + def supports? |
| 53 | + @application.services.one_service? FILTER, KEY_LOCATORS, KEY_USERS |
| 54 | + end |
| 55 | + |
| 56 | + private |
| 57 | + |
| 58 | + FILTER = /session-replication/ |
| 59 | + KEY_LOCATORS = 'locators'.freeze |
| 60 | + KEY_USERS = 'users'.freeze |
| 61 | + |
| 62 | + SESSION_MANAGER_CLASS_NAME = 'org.apache.geode.modules.session.catalina.Tomcat8DeltaSessionManager'.freeze |
| 63 | + REGION_ATTRIBUTES_ID = 'PARTITION_REDUNDANT_HEAP_LRU'.freeze |
| 64 | + CACHE_CLIENT_LISTENER_CLASS_NAME = |
| 65 | + 'org.apache.geode.modules.session.catalina.ClientServerCacheLifecycleListener'.freeze |
| 66 | + SCHEMA_URL = 'http://geode.apache.org/schema/cache'.freeze |
| 67 | + SCHEMA_INSTANCE_URL = 'http://www.w3.org/2001/XMLSchema-instance'.freeze |
| 68 | + SCHEMA_LOCATION = 'http://geode.apache.org/schema/cache http://geode.apache.org/schema/cache/cache-1.0.xsd'.freeze |
| 69 | + LOCATOR_REGEXP = Regexp.new('([^\\[]+)\\[([^\\]]+)\\]').freeze |
| 70 | + FUNCTION_SERVICE_CLASS_NAMES = [ |
| 71 | + 'org.apache.geode.modules.util.CreateRegionFunction', |
| 72 | + 'org.apache.geode.modules.util.TouchPartitionedRegionEntriesFunction', |
| 73 | + 'org.apache.geode.modules.util.TouchReplicatedRegionEntriesFunction', |
| 74 | + 'org.apache.geode.modules.util.RegionSizeFunction' |
| 75 | + ].freeze |
| 76 | + |
| 77 | + private_constant :FILTER, :KEY_LOCATORS, :KEY_USERS, :SESSION_MANAGER_CLASS_NAME, :REGION_ATTRIBUTES_ID, |
| 78 | + :CACHE_CLIENT_LISTENER_CLASS_NAME, :SCHEMA_URL, :SCHEMA_INSTANCE_URL, :SCHEMA_LOCATION, |
| 79 | + :LOCATOR_REGEXP, :FUNCTION_SERVICE_CLASS_NAMES |
| 80 | + |
| 81 | + def add_client_cache(document) |
| 82 | + client_cache = document.add_element 'client-cache', |
| 83 | + 'xmlns' => SCHEMA_URL, |
| 84 | + 'xmlns:xsi' => SCHEMA_INSTANCE_URL, |
| 85 | + 'xsi:schemaLocation' => SCHEMA_LOCATION, |
| 86 | + 'version' => '1.0' |
| 87 | + |
| 88 | + add_pool client_cache |
| 89 | + add_function_service client_cache |
| 90 | + end |
| 91 | + |
| 92 | + def add_functions(function_service) |
| 93 | + FUNCTION_SERVICE_CLASS_NAMES.each do |function_class_name| |
| 94 | + function = function_service.add_element 'function' |
| 95 | + class_name = function.add_element 'class-name' |
| 96 | + class_name.add_text(function_class_name) |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + def add_function_service(client_cache) |
| 101 | + function_service = client_cache.add_element 'function-service' |
| 102 | + add_functions function_service |
| 103 | + end |
| 104 | + |
| 105 | + def add_listener(server) |
| 106 | + server.add_element 'Listener', |
| 107 | + 'className' => CACHE_CLIENT_LISTENER_CLASS_NAME |
| 108 | + end |
| 109 | + |
| 110 | + def add_locators(pool) |
| 111 | + service = @application.services.find_service FILTER |
| 112 | + service['credentials']['locators'].each do |locator| |
| 113 | + match_info = LOCATOR_REGEXP.match(locator) |
| 114 | + pool.add_element 'locator', |
| 115 | + 'host' => match_info[1], |
| 116 | + 'port' => match_info[2] |
| 117 | + end |
| 118 | + end |
| 119 | + |
| 120 | + def add_manager(context) |
| 121 | + context.add_element 'Manager', |
| 122 | + 'className' => SESSION_MANAGER_CLASS_NAME, |
| 123 | + 'enableLocalCache' => 'true', |
| 124 | + 'regionAttributesId' => REGION_ATTRIBUTES_ID |
| 125 | + end |
| 126 | + |
| 127 | + def add_pool(client_cache) |
| 128 | + pool = client_cache.add_element 'pool', |
| 129 | + 'name' => 'sessions', |
| 130 | + 'subscription-enabled' => 'true' |
| 131 | + add_locators pool |
| 132 | + end |
| 133 | + |
| 134 | + def cache_client_xml |
| 135 | + 'cache-client.xml' |
| 136 | + end |
| 137 | + |
| 138 | + def cache_client_xml_path |
| 139 | + @droplet.sandbox + 'conf' + cache_client_xml |
| 140 | + end |
| 141 | + |
| 142 | + def create_cache_client_xml |
| 143 | + document = REXML::Document.new('<?xml version="1.0" encoding="UTF-8"?>') |
| 144 | + add_client_cache document |
| 145 | + write_xml cache_client_xml_path, document |
| 146 | + end |
| 147 | + |
| 148 | + def mutate_context |
| 149 | + puts ' Adding Geode-based Session Replication' |
| 150 | + |
| 151 | + document = read_xml context_xml |
| 152 | + context = REXML::XPath.match(document, '/Context').first |
| 153 | + |
| 154 | + add_manager context |
| 155 | + |
| 156 | + write_xml context_xml, document |
| 157 | + end |
| 158 | + |
| 159 | + def mutate_server |
| 160 | + document = read_xml server_xml |
| 161 | + |
| 162 | + server = REXML::XPath.match(document, '/Server').first |
| 163 | + |
| 164 | + add_listener server |
| 165 | + |
| 166 | + write_xml server_xml, document |
| 167 | + end |
| 168 | + |
| 169 | + def tar_name |
| 170 | + "geode-store-#{@version}.tar.gz" |
| 171 | + end |
| 172 | + |
| 173 | + end |
| 174 | + |
| 175 | + end |
| 176 | +end |
0 commit comments