包装和知名度 [英] Package and visibility

查看:127
本文介绍了包装和知名度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作SDK,我正在尝试将类分离到不同的包,这些类使用其他一些共享类。问题是如果我公开共享类,每个人都能看到它们,而不仅仅是我的类。什么是让他们只能通过我的申请访问它们的正确方法?

I'm making an SDK and I'm trying to separate classes to different packages, those classes use some other shared classes. The issue is if I made the shared classes public everyone will be able to see them, not only my classes. What's the right way to make them only accessible by my application?

示例:

套餐
MyClass1

Package a MyClass1

套餐b
MyClass2

Package b MyClass2

包c
public MySharedClass

Package c public MySharedClass

因为c是公共的MySharedClass将能够访问它,但问题是世界上也会看到它,我怎么能阻止它?

Because c is public MySharedClass will be able to access it, but the issue is that it will also will be visible to the world, how could I prevent that?

推荐答案

非常重要:

共享类可以由一组通常可访问的接口定义。应通过 Classloader 显式加载实际实现。之后,只需应用Java安全管理机制来控制对实现类的访问。任何人都可以看到界面和对实际实现的访问权限仅限于您的SDK。

The shared classes could be defined by a generally accessible set of interfaces. The actual implementation should be loaded explicitly via a Classloader. After that, simply apply Java Security Management mechanisms to control access to the implementation classes. Anyone can see the interfaces and access to actual implementation will be restricted to your SDK.

(以上是每个web / app服务器需要做的变化。你认为Tomcat如何阻止你访问其他应用程序的公共类?)

(A varient of above is what every web/app server needs to do. How do you think Tomcat prevents you from accessing some other app's "public" classes?)

编辑:注意上面是一个运行时机制。还有静态(后)编译方法。例如,APT在这里可能有效。当然,我不是要解决您的软件包重组(在OP中),而只是解决如何保护对一般方法的访问。但这些有点'hacky' - 类加载的运行时机制是规范的,而且imo严格地说更正确。

edit: note above is a runtime mechanism. There are static (post) compile approaches as well. APT for example, could be effective here. Naturally I am not addressing a restructuring of your package (in OP) and only addressing how to secure access to a general approach. But these are a bit 'hacky' -- the runtime mechanism of class loading is canonical and imo strictly more correct.

这篇关于包装和知名度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆