【仅供内部供应商使用,不提供对外解答和培训】

Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagejava
themeEclipse
firstline1
titleRepositoryProfile.java
linenumberstrue
collapsetrue
package com.fr.io.context.info;

import com.fr.cluster.ClusterBridge;
import com.fr.common.annotations.Open;
import com.fr.config.Identifier;
import com.fr.config.holder.Conf;
import com.fr.config.holder.factory.Holders;
import com.fr.config.holder.impl.MapConf;
import com.fr.config.utils.UniqueKey;
import com.fr.io.base.exception.RepositoryException;
import com.fr.io.base.provider.RepositoryFactoryProvider;
import com.fr.io.base.provider.Updatable;
import com.fr.io.config.RepositoryConfig;
import com.fr.io.context.ResourceModuleContext;
import com.fr.stable.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by rinoux on 2018/3/26.
 */
@SuppressWarnings("all")
@Open
public abstract class RepositoryProfile<T extends RepositoryConfig> extends UniqueKey implements Serializable, Updatable {

    /**
     * 仓库类型
     */
    @Identifier("identity")
    private Conf<String> identity = Holders.simple(StringUtils.EMPTY);

    /**
     * 仓库名称
     */
    @Identifier("repoName")
    private Conf<String> repoName = Holders.simple(StringUtils.EMPTY);
    /**
     * 是否集群共享
     */
    @Identifier("shared")
    private Conf<Boolean> shared = Holders.simple(Boolean.FALSE);
    /**
     * 各个机器上的工作路径,如果是共享就共用一个,非共享则各自有各自的workroot,即[id, workroot]
     */
    @Identifier("workroots")
    private MapConf<Map<String, String>> workroots = Holders.map(new HashMap<String, String>(), String.class, String.class);


    public RepositoryProfile() {
    }

    public RepositoryProfile(String identity, String repoName, String workRoot) {
        init(identity, repoName, workRoot, false);
    }


    public RepositoryProfile(String identity, String repoName, String workRoot, T config) {
        init(identity, repoName, workRoot, false);
    }

    public RepositoryProfile(String identity, String repoName, String workRoot, boolean shared) {
        init(identity, repoName, workRoot, shared);
    }


    public RepositoryProfile(String identity, String repoName, String workRoot, boolean shared, T config) {
        init(identity, repoName, workRoot, shared);
    }

    private void init(String identity, String repoName, String workRoot, boolean shared) {
        this.identity.set(identity);
        this.repoName.set(repoName);
        this.shared.set(shared);
        this.setWorkRoot(workRoot);
    }


    public String getIdentity() {
        return identity.get();
    }

    public void setIdentity(String identity) {
        this.identity.set(identity);
    }

    public String getRepoName() {
        return repoName.get();
    }

    public void setRepoName(String repoName) {
        this.repoName.set(repoName);
    }

    public String getWorkRoot() {
        String currentNodeId = ClusterBridge.getView().getCurrent().getID();
        if (workroots.containsKey(currentNodeId)) {
            return (String) workroots.get(currentNodeId);
        }
        else if (isShared()) {
            Iterator<String> iterator = workroots.get().values().iterator();
            while (iterator.hasNext()) {
                return iterator.next();
            }
        }

        return null;
    }

    public void setWorkRoot(String workRoot) {
        String currentNodeId = ClusterBridge.getView().getCurrent().getID();
        if (isShared()) {
            // 共享时改所有的 workroots
            Set<String> ids = workroots.get().keySet();
            for (String id : ids) {
                workroots.put(id, workRoot);
            }
        }
        // 改自己的
        this.workroots.put(currentNodeId, workRoot);
    }

    public boolean isShared() {
        return shared.get();
    }

    public void setShared(boolean shared) {
        this.shared.set(shared);
    }

    public abstract T getConfig();

    public abstract void setConfig(T config);



    @Override
    public RepositoryProfile<T> clone() throws CloneNotSupportedException {
        RepositoryProfile<T> cloned = (RepositoryProfile<T>) super.clone();
        cloned.identity = (Conf<String>) this.identity.clone();
        cloned.repoName = (Conf<String>) this.repoName.clone();
        cloned.shared = (Conf<Boolean>) this.shared.clone();
        cloned.workroots = (MapConf<Map<String, String>>) this.workroots.clone();

        return cloned;
    }

    /**
     * 安装并返回生产的安装组件
     *
     * @return 仓库安装组件
     * @throws RepositoryException
     */
    public InstalledComponent<T> install() {
        return ResourceModuleContext.install(this);
    }


    private boolean validate() {
        if (StringUtils.isEmpty(getIdentity())) {
            return false;
        }

        if (StringUtils.isEmpty(getRepoName())) {
            return false;
        }

        // 不共享但是没有本机的workroot
        if (!isShared() && StringUtils.isEmpty(getWorkRoot())) {
            return false;
        }

        // 共享但是没有workroot配置
        if (isShared() && workroots.get().isEmpty()) {
            return false;
        }

        return true;
    }


    /**
     * 是否适合本机安装
     *
     * @return
     */
    public boolean suitable() {
        if (ResourceModuleContext.getFactory(getIdentity()) == null) {
            return false;
        }
        return validate();
    }

    @Override
    public final void update(String srcRepo) {
        InstalledComponent c = ResourceModuleContext.getInstalledComponent(srcRepo);
        this.setWorkRoot(c.getWorkRoot());
        this.setShared(c.isShared());
        if (getConfig() != null) {
            this.getConfig().update(srcRepo);
        }
    }

    /**
     * 仅生成安装组件(不安装)
     *
     * @return 仓库安装组件
     */
    public final InstalledComponent<T> generateComponent() {
        RepositoryFactoryProvider<T> fac = ResourceModuleContext.getFactory(getIdentity());
        return new InstalledComponent<T>(fac, getRepoName(), getWorkRoot(), isShared());
    }

}



四、支持版本

产品线

版本

支持情况

备注

FR10.0支持

五、插件注册

Code Block
languagexml
themeEclipse
firstline1
titleplugin.xml
linenumberstrue
<extra-core>
        <AlternateResultProvider class="your class name"/>
</extra-core>

...