Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

is it possible to reconfigure the JNPMService with a new password value? #21

Open
bwolfehcm opened this issue Nov 14, 2023 · 1 comment

Comments

@bwolfehcm
Copy link

Basically I am using JNPM to programatically pull down a npm module so I can use some of its files.

if for some reason the file i'm looking for is deleted I want JNPM to pull it down again when the code executes. So I tried something like this.

if(!script_file.exists()){
            JNPMSettings defaultrepo = JNPMSettings.builder()
                .homeDirectory(Paths.get(homePath)) //Optional
                .downloadDirectory(Paths.get(downloadPath)) //Optional
                .installDirectory(Paths.get(installPath))
                .username(config.NPMJS_Username()).password(config.NPMJS_Access_Token())
                .registryUrl(config.NPMJS_Repo_URL())
                .httpLoggerLevel(HttpLoggingInterceptor.Level.BASIC)
                    .build();
            logger.error(String.format("JNPM Configured Password %s", defaultrepo.getPassword()));
            if (!JNPMService.isConfigured()){
                JNPMService.configure(defaultrepo); // maybe the service should be static? can I do that or something?
            }
            else if (!JNPMService.instance().getSettings().getPassword().equals(config.NPMJS_Access_Token())){
                logger.error(String.format("Old PWD: %s New PWD: %s", JNPMService.instance().getSettings().getPassword(),config.NPMJS_Access_Token()));
                JNPMService.configure(defaultrepo);
            }
}

I was hoping I could reconfigure the instance with an updated password, but it seems that the service does not allow for a reconfiguration based on what I am seeing.

public static synchronized JNPMService configure(JNPMSettings settings) {
		if(isConfigured()) throw new IllegalStateException("You can't configure JNPM twise: it's already initiated");
		try {
			settings.createAllDirectories();
			settings.getLogger().log("Settings: "+settings);
			instance = new JNPMService(settings);
			return instance;
		} catch (Exception e) {
			settings.getLogger().log("Can't configure JNPM due to problems with settings", e);
			return null;
		}
	}

So the only way to reconfigure it would be to restart the whole Java program.

@PhantomYdn
Copy link
Member

That's good question... Under the hood there is the following default static method:

static JNPMService instance(JNPMService substitution) {
JNPMService preserved = instance;
instance = substitution;
return preserved;
}

You can call it through reflection. I do understand that it's dirty, but that's the only method I can propose so far without changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants