@Test public void testConfigureOptions() throws Exception { Map<String,String> options = new HashMap<> (); options.put( "cap-drop", "AUDIT_CONTROL" ); options.put( "cap-add", "SYS_PTRACE, SYS_NICE" ); options.put( "hostname", "hello" ); options.put( "AttachStdin", "true" ); options.put( "MemorySwap", "50" ); options.put( "CpuShares", "2" ); options.put( "Memory", "1024" ); options.put( "env", "env1, env2, env3" ); Map<String,String> targetProperties = new HashMap<>( 1 ); targetProperties.put( DockerHandler.IMAGE_ID, "whatever" ); DockerClient dockerClient = DockerUtils.createDockerClient( targetProperties ); CreateContainerCmd cmd = dockerClient.createContainerCmd( "whatever, we won't execute it" ); DockerUtils.configureOptions( options, cmd ); Assert.assertTrue( cmd.isAttachStdin()); Assert.assertEquals( "hello", cmd.getHostName()); Assert.assertTrue( Arrays.deepEquals( new Capability[] { Capability.SYS_PTRACE, Capability.SYS_NICE }, cmd.getCapAdd())); Assert.assertTrue( Arrays.deepEquals( new Capability[] { Capability.AUDIT_CONTROL }, cmd.getCapDrop())); Assert.assertEquals( Long.valueOf( 50L ), cmd.getMemorySwap()); Assert.assertEquals( Long.valueOf( 1024L ), cmd.getMemory()); Assert.assertEquals( Integer.valueOf( 2 ), cmd.getCpuShares()); Assert.assertTrue( Arrays.deepEquals( new String[] { "env1", "env2", "env3" }, cmd.getEnv())); }
public static void configureOptions( Map<String,String> options, CreateContainerCmd cmd ) throws TargetException { Logger logger = Logger.getLogger( DockerUtils.class.getName()); Map<String,List<String>> hackedSetterNames = new HashMap<> (); List<Class<?>> types = new ArrayList<> (); types.add( String.class ); types.add( String[].class ); types.add( long.class ); types.add( Long.class ); types.add( int.class ); types.add( Integer.class ); types.add( boolean.class ); types.add( Boolean.class ); types.add( Capability[].class ); for( Map.Entry<String,String> entry : options.entrySet()) { String optionValue = entry.getValue(); String methodName = entry.getKey().replace( "-", " " ).trim(); methodName = WordUtils.capitalize( methodName ); methodName = methodName.replace( " ", "" ); methodName = "with" + methodName; Method _m = null; for( Method m : cmd.getClass().getMethods()) { boolean sameMethod = methodName.equalsIgnoreCase( m.getName()); boolean methodWithAlias = hackedSetterNames.containsKey( methodName ) && hackedSetterNames.get( methodName ).contains( m.getName()); if( sameMethod || methodWithAlias ) { if( m.getParameterTypes().length != 1 ) { logger.warning( "A method was found for " + entry.getKey() + " but it does not have the right number of parameters." ); continue; } if( ! types.contains( m.getParameterTypes()[ 0 ])) { logger.warning( "A method was found for " + entry.getKey() + " but it does not have the right parameter type. " + "Skipping it. You may want to add a feature request." ); continue; } _m = m; break; } } if( _m == null ) throw new TargetException( "Nothing matched the " + entry.getKey() + " option in the REST API. Please, report it." ); try { Object o = prepareParameter( optionValue, _m.getParameterTypes()[ 0 ]); _m.invoke( cmd, o ); } catch( ReflectiveOperationException | IllegalArgumentException e ) { throw new TargetException( "Option " + entry.getKey() + " could not be set." ); } } }
DockerUtils { public static void configureOptions( Map<String,String> options, CreateContainerCmd cmd ) throws TargetException { Logger logger = Logger.getLogger( DockerUtils.class.getName()); Map<String,List<String>> hackedSetterNames = new HashMap<> (); List<Class<?>> types = new ArrayList<> (); types.add( String.class ); types.add( String[].class ); types.add( long.class ); types.add( Long.class ); types.add( int.class ); types.add( Integer.class ); types.add( boolean.class ); types.add( Boolean.class ); types.add( Capability[].class ); for( Map.Entry<String,String> entry : options.entrySet()) { String optionValue = entry.getValue(); String methodName = entry.getKey().replace( "-", " " ).trim(); methodName = WordUtils.capitalize( methodName ); methodName = methodName.replace( " ", "" ); methodName = "with" + methodName; Method _m = null; for( Method m : cmd.getClass().getMethods()) { boolean sameMethod = methodName.equalsIgnoreCase( m.getName()); boolean methodWithAlias = hackedSetterNames.containsKey( methodName ) && hackedSetterNames.get( methodName ).contains( m.getName()); if( sameMethod || methodWithAlias ) { if( m.getParameterTypes().length != 1 ) { logger.warning( "A method was found for " + entry.getKey() + " but it does not have the right number of parameters." ); continue; } if( ! types.contains( m.getParameterTypes()[ 0 ])) { logger.warning( "A method was found for " + entry.getKey() + " but it does not have the right parameter type. " + "Skipping it. You may want to add a feature request." ); continue; } _m = m; break; } } if( _m == null ) throw new TargetException( "Nothing matched the " + entry.getKey() + " option in the REST API. Please, report it." ); try { Object o = prepareParameter( optionValue, _m.getParameterTypes()[ 0 ]); _m.invoke( cmd, o ); } catch( ReflectiveOperationException | IllegalArgumentException e ) { throw new TargetException( "Option " + entry.getKey() + " could not be set." ); } } } }
DockerUtils { public static void configureOptions( Map<String,String> options, CreateContainerCmd cmd ) throws TargetException { Logger logger = Logger.getLogger( DockerUtils.class.getName()); Map<String,List<String>> hackedSetterNames = new HashMap<> (); List<Class<?>> types = new ArrayList<> (); types.add( String.class ); types.add( String[].class ); types.add( long.class ); types.add( Long.class ); types.add( int.class ); types.add( Integer.class ); types.add( boolean.class ); types.add( Boolean.class ); types.add( Capability[].class ); for( Map.Entry<String,String> entry : options.entrySet()) { String optionValue = entry.getValue(); String methodName = entry.getKey().replace( "-", " " ).trim(); methodName = WordUtils.capitalize( methodName ); methodName = methodName.replace( " ", "" ); methodName = "with" + methodName; Method _m = null; for( Method m : cmd.getClass().getMethods()) { boolean sameMethod = methodName.equalsIgnoreCase( m.getName()); boolean methodWithAlias = hackedSetterNames.containsKey( methodName ) && hackedSetterNames.get( methodName ).contains( m.getName()); if( sameMethod || methodWithAlias ) { if( m.getParameterTypes().length != 1 ) { logger.warning( "A method was found for " + entry.getKey() + " but it does not have the right number of parameters." ); continue; } if( ! types.contains( m.getParameterTypes()[ 0 ])) { logger.warning( "A method was found for " + entry.getKey() + " but it does not have the right parameter type. " + "Skipping it. You may want to add a feature request." ); continue; } _m = m; break; } } if( _m == null ) throw new TargetException( "Nothing matched the " + entry.getKey() + " option in the REST API. Please, report it." ); try { Object o = prepareParameter( optionValue, _m.getParameterTypes()[ 0 ]); _m.invoke( cmd, o ); } catch( ReflectiveOperationException | IllegalArgumentException e ) { throw new TargetException( "Option " + entry.getKey() + " could not be set." ); } } } private  DockerUtils(); }
DockerUtils { public static void configureOptions( Map<String,String> options, CreateContainerCmd cmd ) throws TargetException { Logger logger = Logger.getLogger( DockerUtils.class.getName()); Map<String,List<String>> hackedSetterNames = new HashMap<> (); List<Class<?>> types = new ArrayList<> (); types.add( String.class ); types.add( String[].class ); types.add( long.class ); types.add( Long.class ); types.add( int.class ); types.add( Integer.class ); types.add( boolean.class ); types.add( Boolean.class ); types.add( Capability[].class ); for( Map.Entry<String,String> entry : options.entrySet()) { String optionValue = entry.getValue(); String methodName = entry.getKey().replace( "-", " " ).trim(); methodName = WordUtils.capitalize( methodName ); methodName = methodName.replace( " ", "" ); methodName = "with" + methodName; Method _m = null; for( Method m : cmd.getClass().getMethods()) { boolean sameMethod = methodName.equalsIgnoreCase( m.getName()); boolean methodWithAlias = hackedSetterNames.containsKey( methodName ) && hackedSetterNames.get( methodName ).contains( m.getName()); if( sameMethod || methodWithAlias ) { if( m.getParameterTypes().length != 1 ) { logger.warning( "A method was found for " + entry.getKey() + " but it does not have the right number of parameters." ); continue; } if( ! types.contains( m.getParameterTypes()[ 0 ])) { logger.warning( "A method was found for " + entry.getKey() + " but it does not have the right parameter type. " + "Skipping it. You may want to add a feature request." ); continue; } _m = m; break; } } if( _m == null ) throw new TargetException( "Nothing matched the " + entry.getKey() + " option in the REST API. Please, report it." ); try { Object o = prepareParameter( optionValue, _m.getParameterTypes()[ 0 ]); _m.invoke( cmd, o ); } catch( ReflectiveOperationException | IllegalArgumentException e ) { throw new TargetException( "Option " + entry.getKey() + " could not be set." ); } } } private  DockerUtils(); static DockerClient createDockerClient( Map<String,String> targetProperties ); static void deleteImageIfItExists( String imageId, DockerClient dockerClient ); static Image findImageByIdOrByTag( String name, DockerClient dockerClient ); static Image findImageById( String imageId, List<Image> images ); static Image findImageByTag( String imageTag, List<Image> images ); static Container findContainerByIdOrByName( String name, DockerClient dockerClient ); static ContainerState getContainerState( String containerId, DockerClient dockerClient ); static void configureOptions( Map<String,String> options, CreateContainerCmd cmd ); static Object prepareParameter( String rawValue, Class<?> clazz ); static boolean extractBoolean( Boolean bool ); static String findDefaultImageVersion( String rawVersion ); static String buildContainerNameFrom( String scopedInstancePath, String applicationName ); }
DockerUtils { public static void configureOptions( Map<String,String> options, CreateContainerCmd cmd ) throws TargetException { Logger logger = Logger.getLogger( DockerUtils.class.getName()); Map<String,List<String>> hackedSetterNames = new HashMap<> (); List<Class<?>> types = new ArrayList<> (); types.add( String.class ); types.add( String[].class ); types.add( long.class ); types.add( Long.class ); types.add( int.class ); types.add( Integer.class ); types.add( boolean.class ); types.add( Boolean.class ); types.add( Capability[].class ); for( Map.Entry<String,String> entry : options.entrySet()) { String optionValue = entry.getValue(); String methodName = entry.getKey().replace( "-", " " ).trim(); methodName = WordUtils.capitalize( methodName ); methodName = methodName.replace( " ", "" ); methodName = "with" + methodName; Method _m = null; for( Method m : cmd.getClass().getMethods()) { boolean sameMethod = methodName.equalsIgnoreCase( m.getName()); boolean methodWithAlias = hackedSetterNames.containsKey( methodName ) && hackedSetterNames.get( methodName ).contains( m.getName()); if( sameMethod || methodWithAlias ) { if( m.getParameterTypes().length != 1 ) { logger.warning( "A method was found for " + entry.getKey() + " but it does not have the right number of parameters." ); continue; } if( ! types.contains( m.getParameterTypes()[ 0 ])) { logger.warning( "A method was found for " + entry.getKey() + " but it does not have the right parameter type. " + "Skipping it. You may want to add a feature request." ); continue; } _m = m; break; } } if( _m == null ) throw new TargetException( "Nothing matched the " + entry.getKey() + " option in the REST API. Please, report it." ); try { Object o = prepareParameter( optionValue, _m.getParameterTypes()[ 0 ]); _m.invoke( cmd, o ); } catch( ReflectiveOperationException | IllegalArgumentException e ) { throw new TargetException( "Option " + entry.getKey() + " could not be set." ); } } } private  DockerUtils(); static DockerClient createDockerClient( Map<String,String> targetProperties ); static void deleteImageIfItExists( String imageId, DockerClient dockerClient ); static Image findImageByIdOrByTag( String name, DockerClient dockerClient ); static Image findImageById( String imageId, List<Image> images ); static Image findImageByTag( String imageTag, List<Image> images ); static Container findContainerByIdOrByName( String name, DockerClient dockerClient ); static ContainerState getContainerState( String containerId, DockerClient dockerClient ); static void configureOptions( Map<String,String> options, CreateContainerCmd cmd ); static Object prepareParameter( String rawValue, Class<?> clazz ); static boolean extractBoolean( Boolean bool ); static String findDefaultImageVersion( String rawVersion ); static String buildContainerNameFrom( String scopedInstancePath, String applicationName ); static final String LATEST; }