View Javadoc

1   /**
2    * Copyright (c) 2002-2011 "Neo Technology,"
3    * Network Engine for Objects in Lund AB [http://neotechnology.com]
4    *
5    * This file is part of Neo4j.
6    *
7    * Neo4j is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation, either version 3 of the License, or
10   * (at your option) any later version.
11   *
12   * This program 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
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19   */
20  package org.neo4j.server.webadmin.rest.representations;
21  
22  import java.util.ArrayList;
23  
24  import javax.management.openmbean.CompositeData;
25  
26  import org.neo4j.server.helpers.PropertyTypeDispatcher;
27  import org.neo4j.server.rest.repr.ListRepresentation;
28  import org.neo4j.server.rest.repr.Representation;
29  import org.neo4j.server.rest.repr.ValueRepresentation;
30  
31  /**
32   * Converts common primitive and basic objects and arrays of the same into a representation. Has
33   * additional understanding of CompositeData, to allow representations of JMX beans.
34   */
35  public class JmxAttributeRepresentationDispatcher extends PropertyTypeDispatcher<String, Representation>
36  {
37  
38      @Override
39      protected Representation dispatchBooleanProperty( boolean property, String param )
40      {
41          return ValueRepresentation.bool( property );
42      }
43  
44      @Override
45      protected Representation dispatchDoubleProperty( double property, String param )
46      {
47          return ValueRepresentation.number( property );
48      }
49  
50      @Override
51      protected Representation dispatchFloatProperty( float property, String param )
52      {
53          return ValueRepresentation.number( property );
54      }
55  
56      @Override
57      protected Representation dispatchIntegerProperty( int property, String param )
58      {
59          return ValueRepresentation.number( property );
60      }
61  
62      @Override
63      protected Representation dispatchLongProperty( long property, String param )
64      {
65          return ValueRepresentation.number( property );
66      }
67  
68      @Override
69      protected Representation dispatchShortProperty( short property, String param )
70      {
71          return ValueRepresentation.number( property );
72      }
73  
74      @Override
75      protected Representation dispatchStringProperty( String property, String param )
76      {
77          return ValueRepresentation.string( property );
78      }
79      
80      @Override
81      protected Representation dispatchOtherProperty( Object property, String param ) {
82          if( property instanceof CompositeData) {
83              return new JmxCompositeDataRepresentation( (CompositeData) property );
84          } else {
85              return ValueRepresentation.string( property.toString() );
86          }
87      }
88  
89      @Override
90      protected Representation dispatchOtherArray( Object[] property, String param )
91      {
92          if(property instanceof CompositeData[]) {
93              ArrayList<Representation> values = new ArrayList<Representation>();
94              for(CompositeData value : (CompositeData[]) property) {
95                  values.add( new JmxCompositeDataRepresentation( value ) );
96              }
97              return new ListRepresentation( "", values);
98          } else {
99              return super.dispatchOtherArray(property, param);
100         }
101     }
102     
103     @Override
104     protected Representation dispatchStringArrayProperty( String[] array, String param )
105     {
106         ArrayList<Representation> values = new ArrayList<Representation>();
107         for ( String z : array )
108         {
109             values.add( ValueRepresentation.string( z ) );
110         }
111         return new ListRepresentation( "", values);
112     }
113 
114     @Override
115     @SuppressWarnings( "boxing" )
116     protected Representation dispatchShortArrayProperty( PropertyArray<short[], Short> array,
117             String param )
118     {
119         ArrayList<Representation> values = new ArrayList<Representation>();
120         for ( Short z : array )
121         {
122             values.add( ValueRepresentation.number( z ) );
123         }
124         return new ListRepresentation( "", values);
125     }
126 
127     @Override
128     @SuppressWarnings( "boxing" )
129     protected Representation dispatchIntegerArrayProperty( PropertyArray<int[], Integer> array,
130             String param )
131     {
132         ArrayList<Representation> values = new ArrayList<Representation>();
133         for ( Integer z : array )
134         {
135             values.add( ValueRepresentation.number( z ) );
136         }
137         return new ListRepresentation( "", values);
138     }
139 
140     @Override
141     @SuppressWarnings( "boxing" )
142     protected Representation dispatchLongArrayProperty( PropertyArray<long[], Long> array, String param )
143     {
144         ArrayList<Representation> values = new ArrayList<Representation>();
145         for ( Long z : array )
146         {
147             values.add( ValueRepresentation.number( z ) );
148         }
149         return new ListRepresentation( "", values);
150     }
151 
152     @Override
153     @SuppressWarnings( "boxing" )
154     protected Representation dispatchFloatArrayProperty( PropertyArray<float[], Float> array, String param )
155     {
156 
157         ArrayList<Representation> values = new ArrayList<Representation>();
158         for ( Float z : array )
159         {
160             values.add( ValueRepresentation.number( z ) );
161         }
162         return new ListRepresentation( "", values);
163     }
164 
165     @Override
166     @SuppressWarnings( "boxing" )
167     protected Representation dispatchDoubleArrayProperty( PropertyArray<double[], Double> array,
168             String param )
169     {
170         ArrayList<Representation> values = new ArrayList<Representation>();
171         for ( Double z : array )
172         {
173             values.add( ValueRepresentation.number( z ) );
174         }
175         return new ListRepresentation( "", values);
176     }
177 
178     @Override
179     @SuppressWarnings( "boxing" )
180     protected Representation dispatchBooleanArrayProperty( PropertyArray<boolean[], Boolean> array,
181             String param )
182     {
183         ArrayList<Representation> values = new ArrayList<Representation>();
184         for ( Boolean z : array )
185         {
186             values.add( ValueRepresentation.bool( z ) );
187         }
188         return new ListRepresentation( "", values);
189     }
190 
191     @Override
192     protected Representation dispatchByteProperty( byte property,
193             String param )
194     {
195         throw new UnsupportedOperationException("Representing bytes not implemented.");
196     }
197 
198     @Override
199     protected Representation dispatchCharacterProperty( char property,
200             String param )
201     {
202         return ValueRepresentation.number( property );
203     }
204 }