Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 /*
  * Copyright (C) 2010 JFrog Ltd.
  *
  * Licensed 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 org.jfrog.hudson.util;
When upgrading from an older version, a user might have deployer credentials as local variables of a builder configuration. This converter Will check for existing old deployer credentials and "move" them to a credentials object instead, thus overriding the deployer credentials of the global config.

Author(s):
Noam Y. Tenne
        extends XStream2.PassthruConverter<T> {
        super(xstream);
    }
    @Override
    protected void callback(T deployerOverriderUnmarshallingContext context) {
        Class<? extends DeployerOverrideroverriderClass = deployerOverrider.getClass();
        try {
            Field oldUsernameField = overriderClass.getDeclaredField("username");
            oldUsernameField.setAccessible(true);
            Object oldUsernameValue = oldUsernameField.get(deployerOverrider);
            if (oldUsernameValue != null && StringUtils.isNotBlank((StringoldUsernameValue) &&
                    !deployerOverrider.isOverridingDefaultDeployer()) {
                String oldUsername = (StringoldUsernameValue;
                String oldPassword = null;
                Field oldPasswordField;
                try {
                    oldPasswordField = overriderClass.getDeclaredField("password");
                } catch (NoSuchFieldException e) {
                    oldPasswordField = overriderClass.getDeclaredField("scrambledPassword");
                }
                oldPasswordField.setAccessible(true);
                Object oldPasswordValue = oldPasswordField.get(deployerOverrider);
                if ((oldPasswordValue != null) && StringUtils.isNotBlank(((StringoldPasswordValue))) {
                    oldPassword = Scrambler.descramble((StringoldPasswordValue);
                }
                Field overridingCredentialsField = overriderClass.getDeclaredField("overridingDeployerCredentials");
                overridingCredentialsField.setAccessible(true);
                overridingCredentialsField.set(deployerOverridernew Credentials(oldUsernameoldPassword));
            }
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(getConversionErrorMessage(deployerOverrider), e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(getConversionErrorMessage(deployerOverrider), e);
        }
    }
    private String getConversionErrorMessage(T deployerOverrider) {
        return String.format("Could not convert the class '%s' to use the new overriding deployer credential storage " +
                "format."deployerOverrider.getClass().getName());
    }
New to GrepCode? Check out our FAQ X