001/*
002 * flattr4j - A Java library for Flattr
003 *
004 * Copyright (C) 2011 Richard "Shred" Körber
005 *   http://flattr4j.shredzone.org
006 *
007 * This program is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License / GNU Lesser
009 * General Public License as published by the Free Software Foundation,
010 * either version 3 of the License, or (at your option) any later version.
011 *
012 * Licensed under the Apache License, Version 2.0 (the "License");
013 * you may not use this file except in compliance with the License.
014 *
015 * This program is distributed in the hope that it will be useful,
016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
018 */
019package org.shredzone.flattr4j.spring;
020
021import org.shredzone.flattr4j.FlattrFactory;
022import org.shredzone.flattr4j.FlattrService;
023import org.shredzone.flattr4j.oauth.AccessToken;
024import org.shredzone.flattr4j.oauth.ConsumerKey;
025import org.shredzone.flattr4j.oauth.FlattrAuthenticator;
026
027/**
028 * Default implementation of {@link FlattrServiceFactory}.
029 *
030 * @author Richard "Shred" Körber
031 */
032public class DefaultFlattrServiceFactory implements FlattrServiceFactory {
033
034    private final ConsumerKey consumerKey;
035    private final AccessToken accessToken;
036
037    /**
038     * Creates a new {@link DefaultFlattrServiceFactory} with the given
039     * {@link ConsumerKey}.
040     *
041     * @param consumerKey
042     *            {@link ConsumerKey} to be used by this factory
043     */
044    public DefaultFlattrServiceFactory(ConsumerKey consumerKey) {
045        this(consumerKey, null);
046    }
047
048    /**
049     * Creates a new {@link DefaultFlattrServiceFactory} with the given
050     * {@link ConsumerKey} and {@link AccessToken}.
051     *
052     * @param consumerKey
053     *            {@link ConsumerKey} to be used by this factory
054     * @param accessToken
055     *            {@link AccessToken} to be used by this factory
056     */
057    public DefaultFlattrServiceFactory(ConsumerKey consumerKey, AccessToken accessToken) {
058        if (consumerKey == null) {
059            throw new IllegalArgumentException("A ConsumerKey is required");
060        }
061        this.consumerKey = consumerKey;
062        this.accessToken = accessToken;
063    }
064
065    @Override
066    public FlattrService getFlattrService() {
067        return getFlattrService(accessToken);
068    }
069
070    @Override
071    public FlattrService getFlattrService(AccessToken at) {
072        if (at == null) {
073            throw new IllegalStateException("An AccessToken is required");
074        }
075        return FlattrFactory.getInstance().createFlattrService(at);
076    }
077
078    @Override
079    public FlattrAuthenticator getFlattrAuthenticator() {
080        return new FlattrAuthenticator(consumerKey);
081    }
082
083}