/jboss-as-7.1.1.Final/osgi/service/src/main/java/org/jboss/as/osgi/parser/StartLevelHandler.java
Java | 72 lines | 37 code | 7 blank | 28 comment | 5 complexity | 1c8ccd338d9f8473745e861ab3fc39a3 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0
1/*
2 * JBoss, Home of Professional Open Source.
3 * Copyright 2011, Red Hat, Inc., and individual contributors
4 * as indicated by the @author tags. See the copyright.txt file in the
5 * distribution for a full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */
22package org.jboss.as.osgi.parser;
23
24import org.jboss.as.controller.OperationContext;
25import org.jboss.as.controller.OperationFailedException;
26import org.jboss.as.controller.OperationStepHandler;
27import org.jboss.as.controller.descriptions.ModelDescriptionConstants;
28import org.jboss.dmr.ModelNode;
29import org.jboss.msc.service.ServiceController;
30import org.jboss.osgi.framework.Services;
31import org.osgi.service.startlevel.StartLevel;
32
33/**
34 * Handles the framework start level.
35 *
36 * @author David Bosschaert
37 */
38abstract class StartLevelHandler implements OperationStepHandler {
39 static final StartLevelHandler READ_HANDLER = new StartLevelHandler() {
40 @Override
41 void invokeOperation(StartLevel sls, OperationContext context, ModelNode operation) {
42 int sl = sls.getStartLevel();
43 context.getResult().set(sl);
44 }
45 };
46
47 static final StartLevelHandler WRITE_HANDLER = new StartLevelHandler() {
48 @Override
49 void invokeOperation(StartLevel sls, OperationContext context, ModelNode operation) {
50 int targetStartLevel = operation.require(ModelDescriptionConstants.VALUE).asInt();
51 sls.setStartLevel(targetStartLevel);
52 }
53 };
54
55 @Override
56 public void execute(OperationContext context, ModelNode operation) throws OperationFailedException {
57 ServiceController<?> svc = context.getServiceRegistry(false).getRequiredService(Services.START_LEVEL);
58
59 if (svc == null || svc.getState() != ServiceController.State.UP) {
60 // non-metric read-attribute handlers should not fail
61 // OSGiMessages.MESSAGES.osgiSubsystemNotActive()
62 context.getResult().set(new ModelNode());
63 } else {
64 StartLevel sls = (StartLevel) svc.getValue();
65 invokeOperation(sls, context, operation);
66 }
67
68 context.completeStep();
69 }
70
71 abstract void invokeOperation(StartLevel sls, OperationContext context, ModelNode operation);
72}