Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2005-2014 Red Hat, Inc. Red Hat licenses this file to you under the Apache License, version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package io.fabric8.commands.support;
 
 import java.util.List;
 import java.util.Set;
 import  org.apache.karaf.shell.console.Completer;
 import  org.apache.karaf.shell.console.completer.StringsCompleter;
 
 public class ConfigWithKeyCompleter implements Completer {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(ConfigWithKeyCompleter.class);
 
     private final StringsCompleter delegate = new StringsCompleter();
 
     private ConfigurationAdmin admin;
 
     public void setAdmin(ConfigurationAdmin admin) {
         this. = admin;
     }
 
     public void init() {
         Configuration[] configs;
         try {
             configs = .listConfigurations(null);
             if (configs == null) {
                 return;
             }
         } catch (Exception e) {
             return;
         }
 
         Collection<Stringpids = new ArrayList<String>();
         for (Configuration config : configs) {
             .getStrings().addAll(getPidWithKeys(config.getPid()));
         }
 
 
     }
 
     public int complete(final String bufferfinal int cursorfinal List candidates) {
         int firstPass = .complete(buffercursorcandidates);
         if (firstPass < 0) {
             updateAllPids();
             return .complete(buffercursorcandidates);
         } else {
             return firstPass;
         }
     }

    
Updates all Pids.
 
     private void updateAllPids() {
         Configuration[] configurations = null;
         try {
             configurations = .listConfigurations(null);
             if (configurations != null) {
                 for (Configuration configuration:configurations) {
                       .getStrings().addAll(getPidWithKeys(configuration.getPid()));
                 }
             }
         } catch (Exception e) {
             .warn("Could not lookup pids from configuration admin.");
         }
     }

    
Returns a Set of Stings that contains all keys of the pid prefixed with the pid itself.

Parameters:
pid
Returns:
 
     private Set<StringgetPidWithKeys(String pid) {
         Set<StringpidWithKeys = new LinkedHashSet<String>();
         try {
            Configuration[] configuration = .listConfigurations("(service.pid=" + pid + ")");
            if (configuration != null && configuration.length > 0) {
                Dictionary dictionary = configuration[0].getProperties();
                if (dictionary != null) {
                    Enumeration keyEnumeration = dictionary.keys();
                    while (keyEnumeration.hasMoreElements()) {
                        String key = (StringkeyEnumeration.nextElement();
                        pidWithKeys.add(pid+"/"+key);
                    }
                }
            }
        } catch (IOException e) {
            .warn("Could not lookup pid {} from configuration admin.",pid);
        } catch (InvalidSyntaxException e) {
            .warn("Could not lookup pid {} from configuration admin.",pid);
        }
        return pidWithKeys;
    }
New to GrepCode? Check out our FAQ X