Service.java 15.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 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 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.jmx.JMXService
 *  com.adobe.jmx.statistics.StatisticsProvider
 *  org.apache.commons.io.FileUtils
 *  org.apache.commons.io.FilenameUtils
 *  org.apache.commons.lang.Validate
 *  org.apache.felix.scr.annotations.Activate
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Deactivate
 *  org.apache.felix.scr.annotations.Reference
 *  org.osgi.framework.BundleContext
 *  org.osgi.framework.ServiceRegistration
 *  org.slf4j.Logger
 */
package com.adobe.service;

import com.adobe.aemds.bedrock.CoreConfigService;
import com.adobe.jmx.JMXService;
import com.adobe.jmx.statistics.StatisticsProvider;
import com.adobe.service.*;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.Validate;
import org.apache.felix.scr.annotations.Activate;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Deactivate;
import org.apache.felix.scr.annotations.Reference;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceRegistration;

import javax.management.ObjectInstance;
import javax.management.ObjectName;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.*;

@Component(componentAbstract=1)
public abstract class Service
extends ServiceAPI
implements ServiceMBean {
    private ObjectInstance registeredMBean;
    private ProcessAttributes processAttributesDelegate;
    private ServiceRegistration registration;
    @Reference
    private CoreConfigService coreConfigService;
    @Reference
    private JMXService jmxService;
    private String tempDirPath;
    private String nativeDirPath;
    private String persistentDirPath;
    private BundleContext bundleContext;
    private Map config;
    protected StatisticsProvider mbean;
    protected Map<String, Object> badAttrMap = Collections.synchronizedMap(new HashMap());

    @Activate
    protected final void activate(BundleContext bundleContext, Map config) throws Exception {
        this.bundleContext = bundleContext;
        this.config = config;
        this.initializeStatisticsHost();
        this.initialize();
        this.registerMBean();
    }

    @Deactivate
    protected final void deactivate() {
        this.logger.debug("Deactivating instance {} of service {}", (Object)this, (Object)this.getName());
        this.stop();
        this.unregisterMBean();
    }

    protected abstract void onStartService(Map var1);

    protected void onStopService() {
    }

    public void disableService() {
        this.logger.info("Service {}: Disabling", (Object)this);
        try {
            this.shutdown();
        }
        catch (Exception e) {
            this.logger.warn("Error during service shutdown", (Throwable)e);
        }
    }

    private void bindService(String name, ConnectionFactory cf) {
        BundleContext bc = this.getBundleContext();
        Hashtable<String, String> p = new Hashtable<String, String>();
        p.put("bmc.service.name", name);
        this.registration = bc.registerService(ConnectionFactory.class.getName(), (Object)cf, p);
    }

    public void registerConnectionFactoryManager(ResourceFactoryManager inConnectionFactoryManager) {
        if (inConnectionFactoryManager instanceof ConnectionFactoryManager) {
            this.setConnectionFactoryManager((ConnectionFactoryManager)inConnectionFactoryManager);
        }
        if (inConnectionFactoryManager instanceof ProcessAttributes) {
            this.processAttributesDelegate = (ProcessAttributes)((Object)this.getConnectionFactoryManager());
        }
        String ctxPath = this.getName();
        try {
            if (inConnectionFactoryManager instanceof SingletonFactoryManager) {
                SingletonFactoryManager sfm = (SingletonFactoryManager)inConnectionFactoryManager;
                this.logger.debug("Creating Singleton CF Manager for service {}", (Object)this);
                ConnectionFactory cf = sfm.getConnectionFactory();
                this.logger.debug("Trying to bind ConnectionFactory: " + cf + " to context: " + ctxPath, (Object)this);
                this.bindService(ctxPath, cf);
            } else {
                this.logger.debug("Creating CF Manager for service {}", (Object)this);
                this.logger.debug("Trying to bind ConnectionFactory: " + this.getConnectionFactoryManager().getConnectionFactory() + " to context: " + ctxPath, (Object)this);
                this.bindService(ctxPath, this.getConnectionFactoryManager().getConnectionFactory());
            }
        }
        catch (Throwable e) {
            this.logger.error("Unexpected exception while creating singleton service", e);
        }
    }

    private void unregisterConnectionFactoryManager() {
        try {
            this.logger.debug("Unregistering connection factory {} for service {}", (Object)this, (Object)this.getName());
            if (this.registration != null) {
                this.registration.unregister();
                this.registration = null;
            }
        }
        catch (IllegalStateException e) {
            this.logger.debug("Exception while unregistering connection factory, likely due to the current service being a dummy one: {}", (Throwable)e);
        }
        catch (Throwable exc) {
            this.logger.error("Unexpected exception while unregistering connection factory for service {}: {}", (Object)this.getName(), (Object)exc);
        }
        ConnectionFactoryManager cfm = this.getConnectionFactoryManager();
        if (cfm != null) {
            cfm.onShutdown();
        }
    }

    public File getDeployedFile(String inPath) {
        File deployedFile = null;
        if (File.pathSeparatorChar == '\\') {
            inPath = inPath.replace('/', File.pathSeparatorChar);
        }
        try {
            String parentDir = this.getPersistentDir().getCanonicalPath();
            deployedFile = new File(parentDir, inPath).getAbsoluteFile();
            this.logger.debug("Got DeployedFile: {}, {}", (Object)parentDir, (Object)inPath);
        }
        catch (Throwable exc) {
            this.logger.error("Unexpected exception while getting deployed file", exc);
        }
        return deployedFile;
    }

    public File getTempDir() {
        if (this.tempDirPath == null) {
            this.tempDirPath = this.createDirectory(this.getServerTempDir(), this.getName());
        }
        return new File(this.tempDirPath);
    }

    public File getNativeDir() {
        if (this.nativeDirPath == null) {
            this.nativeDirPath = this.createDirectory(this.getServerNativeDir(), this.getName());
        }
        return new File(this.nativeDirPath);
    }

    public File getPersistentDir() {
        if (this.persistentDirPath == null) {
            this.persistentDirPath = this.createDirectory(this.getServerDataDir(), this.getName());
        }
        return new File(this.persistentDirPath);
    }

    public void setExecutableName(String executableName) {
        this.processAttributesDelegate.setExecutableName(executableName);
    }

    public String getExecutableName() {
        return this.processAttributesDelegate.getExecutableName();
    }

    public void setJVMArgs(String jvmArgs) {
        this.processAttributesDelegate.setJVMArgs(jvmArgs);
    }

    public String getJVMArgs() {
        return this.processAttributesDelegate.getJVMArgs();
    }

    public void setClassPath(String classPath) {
        this.processAttributesDelegate.setClassPath(classPath);
    }

    public String getClassPath() {
        return this.processAttributesDelegate.getClassPath();
    }

    public void setSharedLibraryPaths(String[] paths) {
        this.processAttributesDelegate.setSharedLibraryPaths(paths);
    }

    public String[] getSharedLibraryPaths() {
        return this.processAttributesDelegate.getSharedLibraryPaths();
    }

    public void setExecutableArguments(String args) {
        this.processAttributesDelegate.setExecutableArguments(args);
    }

    public String getExecutableArguments() {
        return this.processAttributesDelegate.getExecutableArguments();
    }

    public boolean getDebug() {
        return this.processAttributesDelegate.getDebug();
    }

    public void setDebug(boolean inDebug) {
        this.processAttributesDelegate.setDebug(inDebug);
    }

    public int getMaximumReUseCount() {
        return this.processAttributesDelegate.getMaximumReUseCount();
    }

    public void setMaximumReUseCount(int count) {
        this.processAttributesDelegate.setMaximumReUseCount(count);
    }

    public String getORBParams() {
        return this.processAttributesDelegate.getORBParams();
    }

    public void setORBParams(String params) {
        this.processAttributesDelegate.setORBParams(params);
    }

    public String getNativeDependencies() {
        return this.processAttributesDelegate.getNativeDependencies();
    }

    public void setNativeDependencies(String nativeDependencies) {
        this.processAttributesDelegate.setNativeDependencies(nativeDependencies);
    }

    public StatisticsProvider getStatisticsProvider() {
        return this.mbean;
    }

    public CoreConfigService getCoreConfigService() {
        Validate.notNull((Object)this.coreConfigService, (String)"CoreConfigService reference not initialized");
        return this.coreConfigService;
    }

    public void setCoreConfigService(CoreConfigService coreConfigService) {
        this.coreConfigService = coreConfigService;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    protected void initialize() throws Exception {
        ClassLoader oldCl = Thread.currentThread().getContextClassLoader();
        try {
            Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
            this.start();
            Object var3_2 = null;
            Thread.currentThread().setContextClassLoader(oldCl);
        }
        catch (Throwable var2_4) {
            Object var3_3 = null;
            Thread.currentThread().setContextClassLoader(oldCl);
            throw var2_4;
        }
    }

    private void start() throws Exception {
        this.setState(SERVICE_STARTING);
        this.setStartTime(System.currentTimeMillis());
        this.logger.debug("Service {}: Starting", (Object)this);
        this.logger.trace("Calling out to service {} onStartService.", (Object)this);
        this.onStartService(this.config);
        this.logger.trace("Returned from service {} onStartService.", (Object)this);
        this.retryBadProperties();
        this.setState(SERVICE_RUNNING);
        this.logger.debug("Service {}: Started", (Object)this);
    }

    private void retryBadProperties() {
        for (String key : this.badAttrMap.keySet()) {
            try {
                this.setAttribute(key, this.badAttrMap.get(key));
            }
            catch (Exception e) {
                this.logger.error("Unexpected exception while setting attribute " + key, (Throwable)e);
            }
        }
    }

    protected void initializePropertiesFromResourceBundle() {
        Properties props = new Properties();
        String bundleName = "com/adobe/config/" + this.getName() + "-init";
        try {
            ResourceBundle labels = ResourceBundle.getBundle(bundleName, Locale.getDefault(), this.getClass().getClassLoader());
            Enumeration<String> names = labels.getKeys();
            while (names.hasMoreElements()) {
                String name = names.nextElement().toString();
                props.put(name, labels.getObject(name));
            }
            this.logger.debug("Loaded properties from {}", (Object)bundleName);
            this.setAttributes(props);
            this.logger.debug("Combined properties table: ", (Object)props.toString());
        }
        catch (MissingResourceException e) {
            this.logger.debug("Exception thrown for missing config properties file: ", (Throwable)e);
            this.logger.debug("Service {}: missing config properties file {}", (Object)this.getName(), (Object)bundleName);
        }
        catch (Exception e) {
            this.logger.error("Service " + this + ": Missing initialization properties " + bundleName, (Throwable)e);
        }
    }

    private void stop() {
        this.setState(SERVICE_STOPPING);
        this.logger.debug("Service {}: Stopping", (Object)this);
        this.logger.trace("Calling out to service {} onStopService.", (Object)this);
        this.onStopService();
        this.logger.trace("Returned from service {} onStopService.", (Object)this);
        this.unregisterConnectionFactoryManager();
        this.deleteTempDir();
        this.setState(SERVICE_STOPPED);
        this.logger.debug("Service {}: Stopped", (Object)this);
    }

    private void registerMBean() {
        try {
            this.registeredMBean = this.jmxService.registerMBeanIfPossible((Object)this.mbean, this.mbean.getObjectName());
        }
        catch (Exception e) {
            this.logger.error("Unexpected exception while registering MBean", (Throwable)e);
            throw new UndeclaredThrowableException(e, "Can't register MBean for " + this);
        }
    }

    private void initializeStatisticsHost() {
        ObjectName objectName = this.jmxService.makeName("AdobeService", this.getName());
        this.mbean = new StatisticsProvider(objectName);
        this.mbean.addReflectionTarget((Object)this);
    }

    private void unregisterMBean() {
        if (this.registeredMBean != null) {
            this.jmxService.unregisterMBean(this.registeredMBean.getObjectName());
        }
    }

    protected void shutdown() {
        this.stop();
    }

    protected BundleContext getBundleContext() {
        Validate.notNull((Object)this.bundleContext, (String)"BundleContext not initialized");
        return this.bundleContext;
    }

    private String getServerDataDir() {
        return this.getCoreConfigService().getServerDataDir();
    }

    private String getServerTempDir() {
        return this.getCoreConfigService().getServerTempDir();
    }

    private String getServerNativeDir() {
        return this.getCoreConfigService().getServerNativeDir();
    }

    private void deleteTempDir() {
        if (this.tempDirPath != null) {
            FileUtils.deleteQuietly((File)new File(this.tempDirPath));
        }
    }

    private String createDirectory(String baseDir, String subDirName) {
        String dir = FilenameUtils.concat((String)baseDir, (String)subDirName);
        File tempDir = new File(dir);
        try {
            FileUtils.forceMkdir((File)tempDir);
            dir = tempDir.getCanonicalPath();
        }
        catch (IOException e) {
            throw new RuntimeException("Error creating directory for Service '" + this.getName() + "' using name '" + dir + "'", e);
        }
        return dir;
    }

    protected void bindCoreConfigService(CoreConfigService coreConfigService) {
        this.coreConfigService = coreConfigService;
    }

    protected void unbindCoreConfigService(CoreConfigService coreConfigService) {
        if (this.coreConfigService == coreConfigService) {
            this.coreConfigService = null;
        }
    }

    protected void bindJmxService(JMXService jMXService) {
        this.jmxService = jMXService;
    }

    protected void unbindJmxService(JMXService jMXService) {
        if (this.jmxService == jMXService) {
            this.jmxService = null;
        }
    }
}