refactored servlet listener

This commit is contained in:
Adrian Cole 2010-08-06 07:16:07 -07:00
parent 0f76bc8b43
commit 05bab93ce8
12 changed files with 43 additions and 143 deletions

View File

@ -28,7 +28,13 @@ public interface ChefService {
void cleanupStaleNodesAndClients(String prefix, int secondsStale);
void createNodeAndPopulateAutomaticAttributes(String nodeName, Iterable<String> runList);
/**
*
* @param nodeName
* @param runList
* @return node sent to the server containing the automatic attributes
*/
Node createNodeAndPopulateAutomaticAttributes(String nodeName, Iterable<String> runList);
void deleteAllNodesInList(Iterable<String> names);

View File

@ -71,8 +71,8 @@ public class BaseChefService implements ChefService {
}
@Override
public void createNodeAndPopulateAutomaticAttributes(String nodeName, Iterable<String> runList) {
createNodeAndPopulateAutomaticAttributes.execute(nodeName, runList);
public Node createNodeAndPopulateAutomaticAttributes(String nodeName, Iterable<String> runList) {
return createNodeAndPopulateAutomaticAttributes.execute(nodeName, runList);
}
@Override

View File

@ -67,7 +67,8 @@ public interface ChefConstants {
*/
public static final String CHEF_LOGGER = "jclouds.chef";
public static final String CHEF_SERVICE_CLIENT = "chef.service.client";
public static final String CHEF_SERVICE_CLIENT = "chef.service-client";
public static final String CHEF_NODE = "chef.node";
public static final String CHEF_ROLE = "chef.role";
public static final String CHEF_NODE_PATTERN = "chef.node-pattern";
public static final String CHEF_RUN_LIST = "chef.run-list";
}

View File

@ -31,7 +31,7 @@ import com.google.inject.ImplementedBy;
*/
@ImplementedBy(CreateNodeAndPopulateAutomaticAttributesImpl.class)
public interface CreateNodeAndPopulateAutomaticAttributes {
void execute(Node node);
Node execute(Node node);
void execute(String nodeName, Iterable<String> runList);
Node execute(String nodeName, Iterable<String> runList);
}

View File

@ -23,7 +23,7 @@ import static com.google.common.base.Predicates.and;
import static com.google.common.base.Predicates.notNull;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.transform;
import static org.jclouds.ohai.Util.OhaiUtils.fromOhaiTime;
import static org.jclouds.chef.util.ChefUtils.fromOhaiTime;
import java.util.Calendar;
import java.util.Date;

View File

@ -61,15 +61,16 @@ public class CreateNodeAndPopulateAutomaticAttributesImpl implements CreateNodeA
}
@Override
public void execute(Node node) {
public Node execute(Node node) {
logger.trace("creating node %s", node.getName());
node.getAutomatic().putAll(automaticSupplier.get());
chef.createNode(node);
logger.debug("created node %s", node.getName());
return node;
}
@Override
public void execute(String nodeName, Iterable<String> runList) {
execute(new Node(nodeName, runList));
public Node execute(String nodeName, Iterable<String> runList) {
return execute(new Node(nodeName, runList));
}
}

View File

@ -1,54 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*/
package org.jclouds.ohai.Util;
import static com.google.common.base.Preconditions.checkNotNull;
import java.util.Date;
import org.jclouds.domain.JsonBall;
import org.jclouds.ohai.Automatic;
import org.jclouds.ohai.config.multibindings.MapBinder;
import com.google.common.base.Supplier;
import com.google.inject.Binder;
import com.google.inject.TypeLiteral;
/**
*
*
* @author Adrian Cole
*/
public class OhaiUtils {
public static Date fromOhaiTime(JsonBall ohaiDate) {
return new Date(Long.parseLong(checkNotNull(ohaiDate, "ohaiDate").toString().replaceAll("\\.[0-9]*$", "")));
}
public static JsonBall toOhaiTime(long millis) {
return new JsonBall(millis + "");
}
public static MapBinder<String, Supplier<JsonBall>> ohaiAutomaticAttributeBinder(Binder binder) {
MapBinder<String, Supplier<JsonBall>> mapbinder = MapBinder.newMapBinder(binder, new TypeLiteral<String>() {
}, new TypeLiteral<Supplier<JsonBall>>() {
}, Automatic.class);
return mapbinder;
}
}

View File

@ -18,8 +18,8 @@
*/
package org.jclouds.ohai.config;
import static org.jclouds.ohai.Util.OhaiUtils.ohaiAutomaticAttributeBinder;
import static org.jclouds.ohai.Util.OhaiUtils.toOhaiTime;
import static org.jclouds.chef.util.ChefUtils.ohaiAutomaticAttributeBinder;
import static org.jclouds.chef.util.ChefUtils.toOhaiTime;
import java.util.Map;
import java.util.Properties;

View File

@ -23,7 +23,7 @@
*/
package org.jclouds.ohai.config;
import static org.jclouds.ohai.Util.OhaiUtils.ohaiAutomaticAttributeBinder;
import static org.jclouds.chef.util.ChefUtils.ohaiAutomaticAttributeBinder;
import static org.testng.Assert.assertEquals;
import java.net.SocketException;

View File

@ -1,54 +0,0 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
*http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
* ====================================================================
*/
package org.jclouds.ohai.util;
import static org.testng.Assert.assertEquals;
import java.util.Date;
import org.jclouds.domain.JsonBall;
import org.jclouds.ohai.Util.OhaiUtils;
import org.testng.annotations.Test;
/**
* Tests behavior of {@code OhaiUtils}
*
* @author Adrian Cole
*/
@Test(groups = "unit", sequential = true, testName = "ohai.OhaiUtilsTest")
public class OhaiUtilsTest {
public static long millis = 1280251180727l;
public static String millisString = "1280251180727";
public static Date now = new Date(1280251180727l);
public void testToOhaiTime() {
assertEquals(OhaiUtils.toOhaiTime(millis).toString(), millisString);
}
public void testFromOhaiTime() {
assertEquals(OhaiUtils.fromOhaiTime(new JsonBall(millisString)), now);
}
}

View File

@ -22,7 +22,8 @@ import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Sets.newHashSet;
import static java.util.Collections.singleton;
import static org.jclouds.chef.reference.ChefConstants.CHEF_NODE;
import static org.jclouds.chef.reference.ChefConstants.CHEF_ROLE;
import static org.jclouds.chef.reference.ChefConstants.CHEF_NODE_PATTERN;
import static org.jclouds.chef.reference.ChefConstants.CHEF_RUN_LIST;
import static org.jclouds.chef.reference.ChefConstants.CHEF_SERVICE_CLIENT;
import java.util.Properties;
@ -34,11 +35,13 @@ import javax.servlet.ServletContextListener;
import org.jclouds.chef.ChefContextFactory;
import org.jclouds.chef.ChefService;
import org.jclouds.chef.domain.Node;
import org.jclouds.chef.reference.ChefConstants;
import org.jclouds.chef.servlet.functions.InitParamsToProperties;
import org.jclouds.logging.Logger;
import org.jclouds.logging.jdk.JDKLogger;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import com.google.inject.AbstractModule;
@ -60,29 +63,26 @@ public class ChefRegistrationListener implements ServletContextListener {
try {
logger.debug("starting initialization");
Properties overrides = InitParamsToProperties.INSTANCE.apply(servletContextEvent.getServletContext());
String role = getInitParam(servletContextEvent, CHEF_ROLE);
logger.trace("creating client connection");
ChefService client = createService(overrides, servletContextEvent);
logger.debug("created client connection");
Node node;
String nodeName;
try {
while (true) {
nodeName = findNextNodeName(client, role);
try {
client.createNodeAndPopulateAutomaticAttributes(nodeName, singleton("role[" + role + "]"));
break;
} catch (IllegalStateException ex) {
logger.debug("client already exists %s: %s", nodeName, ex.getMessage());
}
while (true) {
nodeName = findNextNodeName(client, getInitParam(servletContextEvent, CHEF_NODE_PATTERN));
try {
node = client.createNodeAndPopulateAutomaticAttributes(nodeName, Splitter.on(',').split(
getInitParam(servletContextEvent, CHEF_RUN_LIST)));
break;
} catch (IllegalStateException ex) {
logger.debug("client already exists %s: %s", nodeName, ex.getMessage());
}
} finally {
client.getContext().close();
}
servletContextEvent.getServletContext().setAttribute(CHEF_NODE, nodeName);
servletContextEvent.getServletContext().setAttribute(CHEF_ROLE, role);
servletContextEvent.getServletContext().setAttribute(CHEF_NODE, node);
servletContextEvent.getServletContext().setAttribute(CHEF_SERVICE_CLIENT, client);
logger.debug("initialized");
} catch (RuntimeException e) {
@ -91,13 +91,13 @@ public class ChefRegistrationListener implements ServletContextListener {
}
}
private String findNextNodeName(ChefService client, String prefix) {
private String findNextNodeName(ChefService client, String pattern) {
Set<String> nodes = client.getContext().getApi().listNodes();
String nodeName;
Set<String> names = newHashSet(nodes);
int index = 0;
while (true) {
nodeName = prefix + "-" + index++;
nodeName = String.format(pattern, index++);
if (!names.contains(nodeName))
break;
}
@ -130,9 +130,9 @@ public class ChefRegistrationListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
ChefService client = getContextAttributeOrNull(servletContextEvent, CHEF_SERVICE_CLIENT);
String nodename = getContextAttributeOrNull(servletContextEvent, CHEF_NODE);
if (nodename != null && client != null) {
client.deleteAllNodesInList(singleton(nodename));
Node node = getContextAttributeOrNull(servletContextEvent, CHEF_NODE);
if (node != null && client != null) {
client.deleteAllNodesInList(singleton(node.getName()));
}
if (client != null) {
client.getContext().close();

View File

@ -19,7 +19,7 @@
package org.jclouds.ohai.servlet.config;
import static org.jclouds.ohai.Util.OhaiUtils.ohaiAutomaticAttributeBinder;
import static org.jclouds.chef.util.ChefUtils.ohaiAutomaticAttributeBinder;
import org.jclouds.domain.JsonBall;
import org.jclouds.ohai.config.multibindings.MapBinder;