##### Coverage Report - com.sun.grizzly.utils.ControllerUtils

Classes in this File | Line Coverage | Branch Coverage | Complexity
ControllerUtils |
83 % | 20/24
83 % | 5/6
| 0.0;0
ControllerUtils$1 |
38 % | 3/8
0 % | 0/2
| 0.0;0

1 |  |
     /*
2 |  |
      *
3 |  |
      * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 |  |
      *
5 |  |
      * Copyright 2007-2008 Sun Microsystems, Inc. All rights reserved.
6 |  |
      *
7 |  |
      * The contents of this file are subject to the terms of either the GNU
8 |  |
      * General Public License Version 2 only ("GPL") or the Common Development
9 |  |
      * and Distribution License("CDDL") (collectively, the "License").  You
10 |  |
      * may not use this file except in compliance with the License. You can obtain
11 |  |
      * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
12 |  |
      * or glassfish/bootstrap/legal/LICENSE.txt.  See the License for the specific
13 |  |
      * language governing permissions and limitations under the License.
14 |  |
      *
15 |  |
      * When distributing the software, include this License Header Notice in each
16 |  |
      * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
17 |  |
      * Sun designates this particular file as subject to the "Classpath" exception
18 |  |
      * as provided by Sun in the GPL Version 2 section of the License file that
19 |  |
      * accompanied this code.  If applicable, add the following below the License
20 |  |
      * Header, with the fields enclosed by brackets [] replaced by your own
21 |  |
      * identifying information: "Portions Copyrighted [year]
22 |  |
      * [name of copyright owner]"
23 |  |
      *
24 |  |
      * Contributor(s):
25 |  |
      *
26 |  |
      * If you wish your version of this file to be governed by only the CDDL or
27 |  |
      * only the GPL Version 2, indicate your decision by adding "[Contributor]
28 |  |
      * elects to include this software in this distribution under the [CDDL or GPL
29 |  |
      * Version 2] license."  If you don't indicate a single choice of license, a
30 |  |
      * recipient has the option to distribute your version of this file under
31 |  |
      * either the CDDL, the GPL Version 2 or to extend the choice of license to
32 |  |
      * its licensees as provided above.  However, if you add GPL Version 2 code
33 |  |
      * and therefore, elected the GPL Version 2 license, then the option applies
34 |  |
      * only if the new code is made subject to such option by the copyright
35 |  |
      * holder.
36 |  |
      *
37 |  |
      */
38 |  |
39 |  |
     package com.sun.grizzly.utils;
40 |  |
41 |  |
     import com.sun.grizzly.*;
42 |  |
     import java.io.IOException;
43 |  |
     import java.util.Arrays;
44 |  |
     import java.util.Collection;
45 |  |
     import java.util.concurrent.CountDownLatch;
46 |  |
     import java.util.logging.Level;
47 |  |
48 |  |
     /**
49 |  |
      * @author Alexey Stashok
50 |  |
      */
51 | 0 |
     public class ControllerUtils {
52 |  |
53 |  |
         /**
54 |  |
          *  Start controller in seperate thread
55 |  |
          */
56 |  |
         public static void startController(final Controller controller) {
57 | 39 |
             final CountDownLatch latch = new CountDownLatch(1);
58 | 39 |
             controller.addStateListener(new ControllerStateListenerAdapter() {
59 |  |
                 @Override
60 |  |
                 public void onReady() {
61 | 41 |
                     latch.countDown();
62 | 41 |
                 }
63 |  |
64 |  |
                 @Override
65 |  |
                 public void onException(Throwable e) {
66 | 0 |
                     if (latch.getCount() > 0) {
67 | 0 |
                         Controller.logger().log(Level.SEVERE, "Exception during " +
68 |  |
                                 "starting the controller", e);
69 | 0 |
                         latch.countDown();
70 |  |
                     } else {
71 | 0 |
                         Controller.logger().log(Level.SEVERE, "Exception during " +
72 |  |
                                 "controller processing", e);
73 |  |
                     }
74 | 0 |
                 }
75 |  |
             });
76 |  |
77 | 39 |
             new Thread(controller).start();
78 |  |
79 |  |
             try {
80 | 39 |
                 latch.await();
81 | 0 |
             } catch (InterruptedException ex) {
82 | 39 |
             }
83 |  |
84 | 39 |
             if (!controller.isStarted()) {
85 | 0 |
                 throw new IllegalStateException("Controller is not started!");
86 |  |
             }
87 | 39 |
         }
88 |  |
89 |  |
         /**
90 |  |
          *  Stop controller in seperate thread
91 |  |
          */
92 |  |
         public static void stopController(Controller controller) {
93 |  |
             try {
94 | 8 |
                 controller.stop();
95 | 0 |
             } catch(IOException e) {
96 | 8 |
             }
97 | 8 |
         }
98 |  |
99 |  |
         public static void startControllers(Controller[] controllers) {
100 | 1 |
             startControllers(Arrays.asList(controllers));
101 | 1 |
         }
102 |  |
103 |  |
         public static void startControllers(Collection<Controller> controllers) {
104 | 1 |
             for(Controller controller : controllers) {
105 | 2 |
                 startController(controller);
106 |  |
             }
107 | 1 |
         }
108 |  |
109 |  |
         public static void stopControllers(Controller[] controllers) {
110 | 1 |
             stopControllers(Arrays.asList(controllers));
111 | 1 |
         }
112 |  |
113 |  |
         public static void stopControllers(Collection<Controller> controllers) {
114 | 1 |
             for(Controller controller : controllers) {
115 | 2 |
                 stopController(controller);
116 |  |
             }
117 | 1 |
         }
118 |  |
     }