class SpacewalkService {

    @SuppressWarnings("unchecked")
    public static List<SpacewalkPackage> getPackages(final Credentials credentials,
            final URL spacewalkBaseUrl, final String channelLabel,
            final Repository sourceRepository)
            throws SpacewalkException {
        if (null == sourceRepository) {
            throw new SpacewalkException("must provide sourceRepository");
        }
        if (StringUtils.isEmpty(channelLabel)) {
            throw new SpacewalkException("must provide channelLabel");
        }

        validateParameters(credentials, spacewalkBaseUrl);
        URL queryUrl = getQueryUrl(spacewalkBaseUrl);
        Object sessionKey = getSessionKey(credentials, queryUrl);
        XmlRpcClient client = createXmlRpcClient(queryUrl);

        List<SpacewalkPackage> packageList = new ArrayList<>();

        Object[] rawPackages;
        try {
            rawPackages =
                    (Object[]) client.execute("channel.software.listAllPackages", new Object[] {
                            sessionKey, channelLabel});
            for (Object packagObj : rawPackages) {
                Map<String, Object> packageMap = (Map<String, Object>) packagObj;

                packageList.add(SpacewalkPackage
                        .buildSpacewalkPackage(packageMap, sourceRepository));
            }
        } catch (XmlRpcException e) {
            String message =
                    "Error getting list of spacewalk packages for channel : " + channelLabel + " - "
                            + e.getMessage();
            LOGGER.error(message, e);
            throw new SpacewalkException(message, e);
        } catch (IllegalArgumentException iae) {
            String message =
                    "Error processing channel map returned from Spacewalk for channel: "
                            + channelLabel;
            LOGGER.error(message, iae);
            throw new SpacewalkException(message, iae);
        } finally {
            closeSession(sessionKey, queryUrl);
        }

        return packageList;
    }

    private  SpacewalkService();

}