View Javadoc

1   package org.apache.continuum.distributed.commons.utils;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.util.Map;
23  
24  /**
25   * ContinuumDistributedUtil
26   */
27  public class ContinuumDistributedUtil
28  {
29      public static final String KEY_PROJECT_ID = "project-id";
30  
31      public static final String KEY_PROJECT_GROUP_ID = "project-group-id";
32  
33      public static final String KEY_PROJECT_NAME = "project-name";
34  
35      public static final String KEY_ARTIFACT_ID = "artifact-id";
36  
37      public static int getProjectId( Map<String, Object> context )
38      {
39          return getInteger( context, KEY_PROJECT_ID );
40      }
41  
42      public static int getProjectGroupId( Map<String, Object> context )
43      {
44          return getInteger( context, KEY_PROJECT_GROUP_ID );
45      }
46  
47      public static String getArtifactId( Map<String, Object> context )
48      {
49          return getString( context, KEY_ARTIFACT_ID );
50      }
51  
52      public static String getProjectName( Map<String, Object> context )
53      {
54          return getString( context, KEY_PROJECT_NAME );
55      }
56  
57      public static String getProjectNameAndId( Map<String, Object> context )
58      {
59          StringBuilder result = new StringBuilder();
60  
61          if ( getProjectName( context ) != null )
62          {
63              result.append( getProjectName( context ) ).append( " " );
64          }
65          else if ( getArtifactId( context ) != null )
66          {
67              result.append( getArtifactId( context ) ).append( " " );
68          }
69  
70          if ( context.containsKey( KEY_PROJECT_ID ) )
71          {
72              result.append( "(projectId=" ).append( getProjectId( context ) ).append( ")" );
73          }
74          else
75          {
76              result.append( "(projectGroupId=" ).append( getProjectGroupId( context ) ).append( ")" );
77          }
78  
79          return result.toString();
80      }
81  
82      // ----------------------------------------------------------------------
83      //
84      // ----------------------------------------------------------------------
85  
86      private static String getString( Map<String, Object> context, String key )
87      {
88          Object obj = getObject( context, key, null );
89  
90          if ( obj == null )
91          {
92              return null;
93          }
94          else
95          {
96              return (String) obj;
97          }
98      }
99  
100     private static int getInteger( Map<String, Object> context, String key )
101     {
102         Object obj = getObject( context, key, null );
103 
104         if ( obj == null )
105         {
106             return 0;
107         }
108         else
109         {
110             return (Integer) obj;
111         }
112     }
113 
114     private static Object getObject( Map<String, Object> context, String key, Object defaultValue )
115     {
116         Object value = context.get( key );
117 
118         if ( value == null )
119         {
120             return defaultValue;
121         }
122 
123         return value;
124     }
125 }
126