net.roboconf.core.utils.Utils.readPropertiesFileQuietly()方法的使用及代码示例

x33g5p2x  于2022-02-01 转载在 其他  
字(10.3k)|赞(0)|评价(0)|浏览(130)

本文整理了Java中net.roboconf.core.utils.Utils.readPropertiesFileQuietly()方法的一些代码示例,展示了Utils.readPropertiesFileQuietly()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Utils.readPropertiesFileQuietly()方法的具体详情如下:
包路径:net.roboconf.core.utils.Utils
类名称:Utils
方法名:readPropertiesFileQuietly

Utils.readPropertiesFileQuietly介绍

[英]Reads properties from a file but does not throw any error in case of problem.
[中]从文件中读取属性,但在出现问题时不会抛出任何错误。

代码示例

代码示例来源:origin: net.roboconf/roboconf-dm

private boolean isTargetUsed( String targetId ) {
  File usageFile = findTargetFile( targetId, TARGETS_USAGE_FILE );
  Properties props = Utils.readPropertiesFileQuietly( usageFile, this.logger );
  boolean found = false;
  for( Iterator<Object> it = props.values().iterator(); it.hasNext() && ! found; ) {
    found = it.next().equals( targetId );
  }
  return found;
}

代码示例来源:origin: net.roboconf/roboconf-dm

private void restoreAssociationsCache() {
  File dir = new File( this.configurationMngr.getWorkingDirectory(), ConfigurationUtils.TARGETS );
  for( File f : Utils.listDirectories( dir )) {
    // Store the ID
    this.targetIds.put( f.getName(), Boolean.TRUE );
    // Cache associations for quicker access
    File associationFile = new File( f, TARGETS_ASSOC_FILE );
    Properties props = Utils.readPropertiesFileQuietly( associationFile, this.logger );
    for( Map.Entry<Object,Object> entry : props.entrySet()) {
      InstanceContext key = InstanceContext.parse( entry.getKey().toString());
      this.instanceToCachedId.put( key, f.getName());
    }
  }
}

代码示例来源:origin: net.roboconf/roboconf-dm-scheduler

@Override
public ScheduledJob findJobProperties( String jobId ) {
  this.logger.fine( "Roboconf's scheduler is about to find the properties of the job whose ID is " + jobId );
  ScheduledJob result = null;
  File f = getJobFile( jobId );
  if( f.isFile()) {
    // Inject the ID in the properties
    Properties props = Utils.readPropertiesFileQuietly( f, this.logger );
    props.put( JOB_ID, jobId );
    result = from( props );
  }
  return result;
}

代码示例来源:origin: net.roboconf/roboconf-dm

private List<String> applicationsThatUse( String targetId ) {
  File usageFile = findTargetFile( targetId, TARGETS_USAGE_FILE );
  Properties props = Utils.readPropertiesFileQuietly( usageFile, this.logger );
  List<String> result = new ArrayList<> ();
  for( Object o : props.keySet()) {
    InstanceContext key = InstanceContext.parse((String) o);
    result.add( key.getName());
  }
  return result;
}

代码示例来源:origin: net.roboconf/roboconf-dm

private void saveUsage( InstanceContext mappingKey, String targetId, boolean add )
throws IOException {
  // Usage means the target has been used to create a real machine.
  File usageFile = findTargetFile( targetId, TARGETS_USAGE_FILE );
  Properties props = Utils.readPropertiesFileQuietly( usageFile, this.logger );
  String key = mappingKey.toString();
  if( add )
    props.setProperty( key, targetId );
  else
    props.remove( key );
  writeProperties( props, usageFile );
}

代码示例来源:origin: net.roboconf/roboconf-dm-scheduler

@Override
public List<ScheduledJob> listJobs() {
  this.logger.fine( "Roboconf's scheduler is listing jobs..." );
  List<ScheduledJob> result = new ArrayList<> ();
  for( File f : Utils.listAllFiles( getSchedulerDirectory(), Constants.FILE_EXT_PROPERTIES )) {
    Properties props = Utils.readPropertiesFileQuietly( f, this.logger );
    if( props.isEmpty())
      continue;
    // Inject the ID in the properties
    props.put( JOB_ID, Utils.removeFileExtension( f.getName()));
    ScheduledJob job = from( props );
    result.add( job );
  }
  Collections.sort( result );
  return result;
}

代码示例来源:origin: net.roboconf/roboconf-agent

/**
 * Reconfigures the messaging.
 * @param etcDir the KARAF_ETC directory
 * @param msgData the messaging configuration parameters
 */
public void reconfigureMessaging( String etcDir, Map<String,String> msgData )
throws IOException {
  String messagingType = msgData.get( MessagingConstants.MESSAGING_TYPE_PROPERTY );
  Logger.getLogger( getClass().getName()).fine( "Messaging type for reconfiguration: " + messagingType );
  if( ! Utils.isEmptyOrWhitespaces( etcDir )
      && ! Utils.isEmptyOrWhitespaces( messagingType )) {
    // Write the messaging configuration
    File f = new File( etcDir, "net.roboconf.messaging." + messagingType + ".cfg" );
    Logger logger = Logger.getLogger( getClass().getName());
    Properties props = Utils.readPropertiesFileQuietly( f, logger );
    props.putAll( msgData );
    props.remove( MessagingConstants.MESSAGING_TYPE_PROPERTY );
    Utils.writePropertiesFile( props, f );
    // Set the messaging type
    f = new File( etcDir, Constants.KARAF_CFG_FILE_AGENT );
    props = Utils.readPropertiesFileQuietly( f, Logger.getLogger( getClass().getName()));
    if( messagingType != null ) {
      props.put( Constants.MESSAGING_TYPE, messagingType );
      Utils.writePropertiesFile( props, f );
    }
  }
}

代码示例来源:origin: net.roboconf/roboconf-dm

/**
 * Loads the application bindings into an application.
 * @param app a non-null application
 * @param configurationDirectory the DM's configuration directory
 */
public static void loadApplicationBindings( Application app ) {
  File descDir = new File( app.getDirectory(), Constants.PROJECT_DIR_DESC );
  File appBindingsFile = new File( descDir, APP_BINDINGS_FILE );
  Logger logger = Logger.getLogger( ConfigurationUtils.class.getName());
  Properties props = Utils.readPropertiesFileQuietly( appBindingsFile, logger );
  for( Map.Entry<?,?> entry : props.entrySet()) {
    for( String part : Utils.splitNicely((String) entry.getValue(), "," )) {
      if( ! Utils.isEmptyOrWhitespaces( part ))
        app.bindWithApplication((String) entry.getKey(), part );
    }
  }
}

代码示例来源:origin: net.roboconf/roboconf-dm

List<TargetWrapperDescriptor> buildList( List<File> targetDirectories, AbstractApplication app ) {
  List<TargetWrapperDescriptor> result = new ArrayList<> ();
  for( File targetDirectory : targetDirectories ) {
    File associationFile = new File( targetDirectory, TARGETS_ASSOC_FILE );
    Properties props = Utils.readPropertiesFileQuietly( associationFile, this.logger );
    boolean isDefault = false;
    if( app != null )
      isDefault = props.containsKey( new InstanceContext( app ).toString());
    TargetWrapperDescriptor tb = build( targetDirectory );
    if( tb != null ) {
      tb.setDefault( isDefault );
      result.add( tb );
    }
  }
  return result;
}

代码示例来源:origin: net.roboconf/roboconf-dm

private void saveHint( String targetId, AbstractApplication app, boolean add ) throws IOException {
  // A hint is just a preference (some kind of scope for a target).
  // If a hint is not respected, no exception will be thrown.
  File hintsFile = findTargetFile( targetId, TARGETS_HINTS_FILE );
  Properties props = Utils.readPropertiesFileQuietly( hintsFile, this.logger );
  String key = new InstanceContext( app ).toString();
  if( add ) {
    props.setProperty( key, "" );
  } else {
    props.remove( key );
  }
  writeProperties( props, hintsFile );
}

代码示例来源:origin: net.roboconf/roboconf-dm-scheduler

@Override
public void loadJobs() {
  this.logger.fine( "Roboconf's scheduler is loading jobs..." );
  for( File f : Utils.listAllFiles( getSchedulerDirectory(), Constants.FILE_EXT_PROPERTIES )) {
    try {
      Properties props = Utils.readPropertiesFileQuietly( f, this.logger );
      // Inject the ID in the properties
      props.setProperty( JOB_ID, Utils.removeFileExtension( f.getName()));
      // Validate and reload
      if( validProperties( props ))
        scheduleJob( props );
      else
        this.logger.warning( "Skipped schedule for a job. There are invalid or missing job properties in " + f.getName());
    } catch( Exception e ) {
      // Catch ALL the exceptions. #start() cannot fail.
      this.logger.warning( "Failed to load a scheduled job from " + f.getName());
      Utils.logException( this.logger, e );
    }
  }
}

代码示例来源:origin: net.roboconf/roboconf-dm-scheduler

private void unscheduleJob( String jobId ) throws IOException {
  File f = getJobFile( jobId );
  try {
    if( f.exists()) {
      Properties props = Utils.readPropertiesFileQuietly( f, this.logger );
      String appName = props.getProperty( APP_NAME, "" );
      if( ! Utils.isEmptyOrWhitespaces( appName ))
        this.scheduler.unscheduleJob( TriggerKey.triggerKey( jobId, appName ));
    }
  } catch( SchedulerException e ) {
    // Catch all the exceptions (including the runtime ones)
    throw new IOException( e );
  } finally {
    Utils.deleteFilesRecursively( f );
  }
}

代码示例来源:origin: net.roboconf/roboconf-dm

private void saveAssociation( AbstractApplication app, String targetId, String instancePathOrComponentName, boolean add )
throws IOException {
  // Association means an exact mapping between an application instance
  // and a target ID.
  InstanceContext key = new InstanceContext( app, instancePathOrComponentName );
  // Remove the old association, always.
  if( instancePathOrComponentName != null ) {
    String oldTargetId = this.instanceToCachedId.remove( key );
    if( oldTargetId != null ) {
      File associationFile = findTargetFile( oldTargetId, TARGETS_ASSOC_FILE );
      Properties props = Utils.readPropertiesFileQuietly( associationFile, this.logger );
      props.remove( key.toString());
      writeProperties( props, associationFile );
    }
  }
  // Register a potential new association and update the cache.
  if( add ) {
    File associationFile = findTargetFile( targetId, TARGETS_ASSOC_FILE );
    if( associationFile == null )
      throw new IOException( "Target " + targetId + " does not exist." );
    Properties props = Utils.readPropertiesFileQuietly( associationFile, this.logger );
    props.setProperty( key.toString(), "" );
    writeProperties( props, associationFile );
    this.instanceToCachedId.put( key, targetId );
  }
}

代码示例来源:origin: net.roboconf/roboconf-dm

Properties props = Utils.readPropertiesFileQuietly( f, this.logger );
props.remove( key.toString());
writeProperties( props, f );

代码示例来源:origin: net.roboconf/roboconf-dm

@Override
public List<TargetWrapperDescriptor> listPossibleTargets( AbstractApplication app ) {
  // Find the matching targets based on registered hints
  String key = new InstanceContext( app ).toString();
  String tplKey = null;
  if( app instanceof Application )
    tplKey = new InstanceContext(((Application) app).getTemplate()).toString();
  List<File> targetDirectories = new ArrayList<> ();
  File dir = new File( this.configurationMngr.getWorkingDirectory(), ConfigurationUtils.TARGETS );
  for( File f : Utils.listDirectories( dir )) {
    // If there is no hint for this target, then it is global.
    // We can list it.
    File hintsFile = new File( f, TARGETS_HINTS_FILE );
    if( ! hintsFile.exists()) {
      targetDirectories.add( f );
      continue;
    }
    // Otherwise, the key must exist in the file
    Properties props = Utils.readPropertiesFileQuietly( hintsFile, this.logger );
    if( props.containsKey( key ))
      targetDirectories.add( f );
    else if( tplKey != null && props.containsKey( tplKey ))
      targetDirectories.add( f );
  }
  // Build the result
  return buildList( targetDirectories, app );
}

代码示例来源:origin: roboconf/roboconf-platform

@Test
public void testReadPropertiesFileQuietly() throws Exception {
  File f = this.folder.newFile();
  Utils.writeStringInto( "prop: op", f );
  Logger logger = Logger.getLogger( getClass().getName());
  // Normal
  Properties props = Utils.readPropertiesFileQuietly( f, logger );
  Assert.assertEquals( 1, props.size());
  Assert.assertEquals( "op", props.get( "prop" ));
  // Inexisting file
  props = Utils.readPropertiesFileQuietly( new File( "inexisting" ), logger );
  Assert.assertEquals( 0, props.size());
  // Null file
  props = Utils.readPropertiesFileQuietly( null, logger );
  Assert.assertEquals( 0, props.size());
}

相关文章